python数据帧-删除不需要的unicode行

python数据帧-删除不需要的unicode行,python,pandas,unicode,Python,Pandas,Unicode,我在数据框中有一列“name”。 我想删除所有行,它是名称而不是名称中的所有行: names_all = ['alice', 'bob', 'david'] 名称和数据框中的所有字符串都是unicode字符串 我写了代码: for index, row in history.iterrows(): if row['name'] not in names_all: history.drop(index, inplace=True) 但出于某种原因,它在做一些奇怪的事情。

我在数据框中有一列“name”。 我想删除所有行,它是名称而不是名称中的所有行:

names_all = ['alice', 'bob', 'david']
名称和数据框中的所有字符串都是unicode字符串

我写了代码:

for index, row in history.iterrows():
    if row['name'] not in names_all:
        history.drop(index, inplace=True)
但出于某种原因,它在做一些奇怪的事情。它删除了太多行(删除了所有1700行以及更多行)。
在我将所有字符串编码为unicode之前,它也做了一些奇怪的事情,但没有那么多。

解决方案1:

history = history[history['name'].isin(names_all)]
history = history.query("name in @names_all"]
解决方案2:

history = history[history['name'].isin(names_all)]
history = history.query("name in @names_all"]

解决方案1:

history = history[history['name'].isin(names_all)]
history = history.query("name in @names_all"]
解决方案2:

history = history[history['name'].isin(names_all)]
history = history.query("name in @names_all"]

为什么不选择那些与
名称匹配的名称呢

history = history.loc[history.name.isin(names_all)]

为什么不选择那些与
名称匹配的名称呢

history = history.loc[history.name.isin(names_all)]

我不知道这种方法,但它不起作用。它弄乱了整个数据框。@sheldonzy,对不起,我误解了你的问题。请现在检查一下……我不知道这种方法,但它不起作用。它弄乱了整个数据框。@sheldonzy,对不起,我误解了你的问题。请现在检查……在我看来,他想删除列表中不存在的行,他的代码使用
不在
在我看来,他想删除列表中不存在的行,他的代码使用
不在