且构网

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

通过不在列表中的索引值对Pandas数据框进行切片

更新时间:2022-11-01 18:01:39

使用 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