您可以使用str.contains()方法来筛选出包含特定字符串的行,并使用~运算符来取反,最后使用drop()方法来删除这些行。下面是实现的代码:

import pandas as pd

# 创建示例数据
data = {'index': ['Hen eggs in shell, fresh', 'Fruit Primary', 'Meat, Poultry', 'Meat of chickens, fresh or chilled', 'Roots and Tubers, Total'],
        'Item': [1181, 724, 723, 723, 721]}
df = pd.DataFrame(data)

# 删除包含'Total'和'all'的行
df = df[~df['index'].str.contains('Total|all')]

print(df)

这将输出:

                              index  Item
0         Hen eggs in shell, fresh  1181
1                    Fruit Primary   724
2                    Meat, Poultry   723
3  Meat of chickens, fresh or chilled   723
index Item0 Hen eggs in shell fresh 11811 Fruit Primary 7242 Meat Poultry 7233

原文地址: https://www.cveoy.top/t/topic/i5Mx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录