Page 204 - Touhpad Ai
P. 204

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 10: 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 11: 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],
                       'Address':['Delhi', 'Kanpur', 'Meerut', 'Indore'],


                 202    Touchpad Artificial Intelligence - XI
   199   200   201   202   203   204   205   206   207   208   209