Python 获取真实值的系列标签,而无需将系列存储在temp变量中

Python 获取真实值的系列标签,而无需将系列存储在temp变量中,python,pandas,Python,Pandas,输入: 预期结果: 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中,并使用它两次 我正在寻找的解决方案是 s = pd.Series([True, False, True, False], inde

输入:

预期结果:

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中,并使用它两次

我正在寻找的解决方案是

s = pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd'])
s[s == True].index
但是返回真值的标签,而不是它们的整数索引

笔记:
这个问题类似于,但更具体。

我总是转到
loc

np.where(s)

啊哈!回答得很好。“那么熊猫身上就没有这种天赋了?”亚历克斯莱内尔我不这么认为~
pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd']).loc[lambda x : x].index
Index(['a', 'c'], dtype='object')