【发布时间】:2020-10-18 05:38:10
【问题描述】:
输入:
pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd'])
想要的结果:
['a', 'c'] # can be in pd.Series or np.array format, doesn't matter.
但是我想要一个不将系列存储在临时变量中的解决方案,例如
s = pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd'])
s[s == True].index
将系列存储在s中,并使用它两次。
我正在寻找的解决方案是这样的
np.where(s)
但返回真实值的标签,而不是它们的整数索引。
注意事项:
这个问题类似于this one,但更具体。
【问题讨论】: