How do I select only True values?
myindex=['a', 'b', 'c' , 'd']
myseries=pd.Series([True, True, False, True], index=myindex)
a True
b True
c False
d True
dtype: bool
What I have tried:
myseries.where(myseries == True)
This includes "c" while I need to return a list of a, b and d
If you just want to return the index which is a, b, c, d in your case use the index
attribute:
myindex=['a', 'b', 'c' , 'd']
myseries=pd.Series([True, True, False, True], index=myindex)
a True
b True
c False
d True
dtype: bool
myseries[myseries].index
>> Index(['a', 'b', 'd'], dtype='object')
If you want it as a list:
myseries[myseries].index.tolist()
>> ['a', 'b', 'd']
The code myseries[myseries]
returns
a True
b True
d True
dtype: bool
If you specifically want a list of ['a', 'b', 'd'] then you can it by list(myseries[myseries].index)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With