我想使用pandas基于我的给定列表删除某些行,但也要跳过那些不包含在我的数据框中的项目。
例如,
# My dataframe
id,name
A,Bill
B,Lee
C,Jack
# id list that I want to take out
id,
A,
E, # does not contain in data frame
F, # does not contain in data frame
G, # does not contain in data frame
## I'd like to see in my result...
id,name
B,Lee
C,Jack
我尝试了df[~df['id'].isin(given_id_list)]
和df.set_index('id').drop(given_id_list.set_index('id').index)
,但两者都效果不佳。
有任何明智的建议吗?
答案 0 :(得分:1)
这不是最“pythonic”的解决方案 - 但它确实有效。遍历索引,检查ID变量,如果它在列表中,则将其删除。
import pandas as pd
#create the sample dataframe
data = {'id':['A','B','C'], 'name': ['jack', 'john', 'bill']}
df = pd.DataFrame(data)
#list of possible rows to drop:
to_drop = ['A', 'F', 'G']
#loop thru each index
for ix in df.index:
#check if it is a good one to drop
if df.loc[ix]['id'] in to_drop:
#drop it if it is
df.drop(ix, inplace = True)