I am working on a Pandas Dataframe which has boolean values in it's cells. I want to find out the header and index of all cells in the dataframe where the value is False
(Expected output is header_index
)
df_sample=pandas.DataFrame([[True,True,False,True],[False,True,False,True],[True,True,True,True],[False,True,True,True]],columns=['ColumnA','ColumnB','ColumnC','ColumnD'])
ColumnA ColumnB ColumnC ColumnD
0 True True False True
1 False True False True
2 True True True True
3 False True True True
My expected output is:
ColumnC_0
ColumnA_1
ColumnC_1
I have tried multiple iterations using loc
and iloc
but unable to come up with this formatted output A logical idea on how to approach this would help
Use DataFrame.stack
to reshape the dataframe df_sample
into a MultiIndex
series s
, then use boolean masking to filter the series next use map
to flatten the MultiIndex
to get the desired list:
s = (~df_sample).stack()
idx = s[s].index.map(lambda s: f'{s[1]}_{s[0]}').tolist()
Result:
print(idx)
['ColumnC_0', 'ColumnA_1', 'ColumnC_1', 'ColumnA_3']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.