Page 213 - AI_Ver_3.0_class_11
P. 213

Output:
                          Name  Age  Address  Qualification
                     0    Adit   27    Delhi                M.Sc.
                     1    Ekam   24   Kanpur                   MA
                     2  Sakshi   25   Meerut                  MCA
                     3     Anu   30   Indore                Ph.D.

                 Selecting a Column from the DataFrame
                 You can select specific columns from a DataFrame using their labels (column names). This allows you to focus on relevant
                 data for analysis.

                  Program 45: To select a column from the 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)
                     # Displaying DataFrame with Selected Column
                     print(df[['Name', 'Age', 'Qualification']])
                 Output:
                           Name       Age  Qualification

                     0     Adit        27             M.Sc.
                     1     Ekam        24                MA
                     2  Sakshi         25               MCA
                     3       Anu       30             Ph.D.

                 Selecting a Row from the DataFrame
                 You can select specific rows based on their indices or use boolean indexing based on conditions. This enables you to
                 filter the data based on certain criteria, such as values meeting a particular condition. To select a particular row from
                 the DataFrame, you can use the .iloc[] method. This method is used for integer-location based indexing, which means
                 you select rows and columns by their position (index).

                  Program 46: To select a row using .iloc[] method

                     # Import pandas library
                     import pandas as pd

                     # Define a dictionary containing employee data
                     data = {'Name':['Adit', 'Ekam', 'Sakshi', 'Anu'],
                             'Age':[27, 24, 25, 30],


                                                                                         Python Programming     211
   208   209   210   211   212   213   214   215   216   217   218