Page 214 - Ai_V3.0_c11_flipbook
P. 214

'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]
                   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 47: 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)




                    212     Touchpad Artificial Intelligence (Ver. 3.0)-XI
   209   210   211   212   213   214   215   216   217   218   219