过滤熊猫数据框以根据列表下降

时间:2019-08-05 09:49:58

标签: python string pandas filter

我有一个这样的数据框,其中一列包含国家/地区,而其他列(与该问题无关),如下所示。

data = {"country": ["AA", "BB", "AA", "CC", "DD", "AA", "BB", "AA", "CC", "DD"],
"other variable": ["foo", "bar", "bla", "house", "fish", "car", "pet", "dog", "cat", "door"]}

df = pd.DataFrame(data)
to_drop = ['AA', 'CC']

我现在想过滤国家/地区列,并删除列表中的所有(此处,国家“ AA”和“ CC”,实际上还有更多)。

对于一个国家,我只会使用df_new = df[df['country'].apply(lambda x: "AA" not in x)],它可以正常工作-但我不知道如何在包含要摆脱的国家的列表to_drop = ['AA', 'CC']上进行迭代。

有人看不到一个优雅的解决方案吗?我已经检查了所有“过滤熊猫”问题,但均未成功。

2 个答案:

答案 0 :(得分:1)

我想到的唯一(不是很优雅的方式)是

for i in to_drop:
    df_new = df[df['country'].apply(lambda x: i not in x)]

答案 1 :(得分:0)

我测试了这是可行的

df_new=df[~df.country.isin(to_drop)]