How to delete rows with specified values in a dataframe…

To remove rows with specified values in a DataFrame, you can use the drop() method and boolean indexing of the DataFrame. The specific steps are as follows:

  1. Use boolean indexing to find all rows that contain a specific value.
  2. eliminate

Here is an example code:

import pandas as pd

# 创建DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8],
        'C': [9, 10, 11, 12]}
df = pd.DataFrame(data)

# 打印原始DataFrame
print("Original DataFrame:")
print(df)

# 指定要删除的值
value_to_delete = 2

# 使用布尔索引找到所有包含指定值的行
rows_to_delete = df[df['A'] == value_to_delete].index

# 删除指定值的行
df = df.drop(rows_to_delete)

# 打印删除指定值后的DataFrame
print("\nDataFrame after deleting rows with value %d:" % value_to_delete)
print(df)

In the example above, we created a DataFrame first, then specified the value to be deleted as 2. Next, we used boolean indexing to find all rows containing the value 2, and then used the drop() method to delete these rows. Finally, we printed the DataFrame after deleting the specified value.

bannerAds