How to delete certain rows in a pandas table?

To delete certain rows from a table, you can use the drop() function.

Here are some sample codes:

  1. Delete a single line.
df.drop(index=3, inplace=True)

This will remove the row with index 3. Setting inplace=True means that the modification will be made on the original DataFrame.

  1. Delete multiple lines:
df.drop(index=[2, 4, 6], inplace=True)

This will remove the rows with indexes 2, 4, and 6.

  1. Delete rows based on conditions.
df.drop(df[df['column_name'] > 10].index, inplace=True)

This will remove all rows in the column named “column_name” with values greater than 10.

Note that the drop() function by default returns a new DataFrame, to modify the original DataFrame, you need to set the inplace=True parameter.

广告
Closing in 10 seconds
bannerAds