且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按列表中(不在)中的索引值对 Pandas 数据框进行切片

更新时间:2023-01-23 07:56:00

使用 isin 在索引上并反转布尔索引以执行标签选择:

Use isin on the index and invert the boolean index to perform label selection:

In [239]:

df = pd.DataFrame({'a':np.random.randn(5)})
df
Out[239]:
          a
0 -0.548275
1 -0.411741
2 -1.187369
3  1.028967
4 -2.755030
In [240]:

t = [2,4]
df.loc[~df.index.isin(t)]
Out[240]:
          a
0 -0.548275
1 -0.411741
3  1.028967