Page 205 - Touhpad Ai
P. 205

'Qualification':['M.Sc.', 'MA', 'MCA', 'Ph.D.']}

                   # Convert the dictionary into DataFrame
                   df = pd.DataFrame(data)

                   # Selecting the second row (index 1)

                   selected_row = df.iloc[2]
                   print(selected_row)
                   Output:
                   Name              Sakshi

                   Age                   25
                   Address           Meerut

                   Qualification        MCA

                   Name: 2, dtype: object
                 To print the output in the same format as the original DataFrame (df), you can use the to_dict() method along with
                 orient='records' to convert the selected row into a dictionary and then create a new DataFrame from that dictionary.

                    Program  12: To select a  row using  .iloc[] method  and  print  the output in  the same format  as the original
                    DataFrame

                   # Import pandas library
                   import pandas as pd

                   # Define a dictionary containing employee data
                   data = {'Name':['Adit', 'Ekam', 'Sakshi', 'Anu'],

                             'Age':[27, 24, 25, 30],
                             'Address':['Delhi', 'Kanpur', 'Meerut', 'Indore'],
                            'Qualification':['M.Sc.', 'MA', 'MCA', 'Ph.D.']}

                   # Convert the dictionary into DataFrame

                   df = pd.DataFrame(data)

                   # Selecting the second row (index 1)
                   selected_row = df.iloc[2]


                   # Convert the selected row to a dictionary
                   selected_row_dict = selected_row.to_dict()

                   # Create a new DataFrame from the selected row dictionary
                   selected_row_df = pd.DataFrame([selected_row_dict])


                   # Display the new DataFrame
                   print(selected_row_df)
                   Output:

                        Name        Age  Address  Qualification

                   0            Sakshi         25            Meerut       MCA
                                                                      Theoretical and Practical Aspects of Data Processing  203
   200   201   202   203   204   205   206   207   208   209   210