1.

How will you delete indices, rows and columns from a dataframe?

Answer»

To delete an Index:

  • Execute del df.index.name for removing the index by name.
  • Alternatively, the df.index.name can be assigned to None.
  • For example, if you have the below dataframe:
Column 1 NAMES John 1 Jack 2 Judy 3 Jim 4
  • To drop the index name “Names”:
df.index.name = None# Or run the below:# del df.index.nameprint(df) Column 1John 1Jack 2Judy 3Jim 4

To delete row/column from dataframe:

  • drop() METHOD is used to delete row/column from dataframe.
  • The axis argument is passed to the drop method where if the value is 0, it indicates to drop/delete a row and if 1 it has to drop the column.
  • Additionally, we can try to delete the rows/columns in place by SETTING the value of inplace to True. This makes sure that the job is done without the need for reassignment.
  • The duplicate values from the row/column can be deleted by USING the drop_duplicates() method.


Discussion

No Comment Found