Python 输出一系列布尔值为真
我有一个这样的系列Python 输出一系列布尔值为真,python,pandas,series,Python,Pandas,Series,我有一个这样的系列 s = pd.Series({'a': False, 'b': True, 'c': False, 'd': True, 'e': False}) >> s a False b True c False d True e False dtype: bool 有没有一种方法可以巧妙地提取出真实的名称,即在熊猫或NumPy中,而不回到普通的Python中 目前我正在使用这个: sdict = s.to_dict() for ite
s = pd.Series({'a': False, 'b': True, 'c': False, 'd': True, 'e': False})
>> s
a False
b True
c False
d True
e False
dtype: bool
有没有一种方法可以巧妙地提取出真实的名称,即在熊猫或NumPy中,而不回到普通的Python中
目前我正在使用这个:
sdict = s.to_dict()
for item in list(sdict):
if sdict[item] == True:
print (item, end=" ")
>> b d
与s.index一起使用
:
print (s.index[s])
Index(['b', 'd'], dtype='object')
print (s.index[s].tolist())
['b', 'd']
print (', '.join(s.index[s]))
b, d
一个带有np的稍微过于复杂的解决方案。其中
,为了好玩:
print (s.index[np.where(s)[0]])
Index(['b', 'd'], dtype='object')
s.index[s]
就可以了。