2024-05-07 14:10:47 发布
网友
我想知道如何从熊猫系列中排除一个或多个项目。例如:
s = pd.Series(data=range(10), index=[chr(ord('A') + x) for x in range(10)])
现在我想排除B、D、E行
一种极其低效的方法是:
有更好的办法吗?在
谢谢。在
您可以使用index^{}方法:
In [11]: s.index.isin(list('BDE')) Out[11]: array([False, True, False, True, True, False, False, False, False, False], dtype=bool)
使用invert运算符求反(因此它现在读作“not in”):
用这个来遮罩这个系列:
In [13]: s = s[~s.index.isin(list('BDE'))] In [14]: s Out[14]: A 0 C 2 F 5 G 6 H 7 I 8 J 9 dtype: int64
您可以使用index^{} 方法:
使用invert运算符求反(因此它现在读作“not in”):
^{pr2}$用这个来遮罩这个系列:
相关问题 更多 >
编程相关推荐