Page 210 - Touhpad Ai
P. 210

# Add a new column 'Salary' to the DataFrame
                 df['Salary'] = [50000, 45000, 55000, 60000]

                 # Deleting a Row
                 df.drop(index=1, inplace=True)  # Deleting the row with index 1 (Ekam)
                 print("\nDataFrame after deleting a row:")
                 print(df)

                 # Deleting a Column
                 df.drop(columns='Address', inplace=True)  # Deleting the 'Address' column

                 print("DataFrame after deleting a column:")
                 print(df)
                 Output:
                DataFrame after deleting a row:
                       Name    Age   Address  Qualification  Salary
                0      Adit     27     Delhi               M.Sc.    50000
                2    Sakshi     25    Meerut                  MCA     55000
                3       Anu     30    Indore               Ph.D.    60000

              DataFrame after deleting a column:
                        Name    Age   Qualification  Salary
                 0      Adit     27               M.Sc.    50000
                 2    Sakshi     25                 MCA    55000
                 3       Anu     30               Ph.D.    60000
              To delete multiple columns with specific labels, you can modify the drop method to include a list of column labels.
              DataFrame.drop() method can also be used to remove the duplicate rows.

                 # Deleting Columns with specific labels
                 labels_to_delete = ['Age', 'Address'] #specify in a list
                 df.drop(columns=labels_to_delete, inplace=True)

                   AI REBOOT

                  1.  Fill in the blanks.
                     a.  The two data structures that are supported by Pandas are          and                .

                     b.  The               library in Python excels in creating N-dimension data objects.
                     c.  The statement to install NumPy is          .
                     d.  You can check the shape of an array by using the          method in NumPy.
                  2.  Answer the following questions:

                     a.  What is a DataFrame in Pandas?


                     b.  Give one advantage of using NumPy arrays over lists.












                 208    Touchpad Artificial Intelligence - XI
   205   206   207   208   209   210   211   212   213   214   215