如何选择数据框中不在pandas系列数组中的行?

问题描述 投票:0回答:2
df = pd.DataFrame({'x':[1,2,3,4,5,6],'y':[7,8,9,10,11,12]})
index=pd.Series([2,5])

如何使用df中没有的索引在index中选择行?

df.loc[~index,:]df.loc[not(index),:]不起作用

python pandas subset
2个回答
1
投票

你可以简单地使用df[~df.index.isin(index)]。现在请记住,数据帧从索引0开始。


1
投票

这是索引difference的功能之一

df.loc[df.index.difference(index)]
   x   y
0  1   7
1  2   8
3  4  10
4  5  11
© www.soinside.com 2019 - 2024. All rights reserved.