Let's say we have a DataFrame:
df = pd.DataFrame({
'D': {'A': 0.810032, 'B': 0.752299, 'C': 0.224038},
'E': {'A': 0.17714, 'B': 0.680405, 'C': 0.769732},
'F': {'A': 0.942959, 'B': 0.436582, 'C': 0.269791}
})
print(df)
D E F
A 0.810032 0.177140 0.942959
B 0.752299 0.680405 0.436582
C 0.224038 0.769732 0.269791
Is it possible to return index and column names into a list of tuples based on a condition, for instance
print(df < 0.25)
D E F
A False True False
B False False False
C True False False
I would like to end up with a list of tuples like:
[('A','E'),('C','D')]
We can also use DataFrame.where then stack which takes advantage of the fact that stack drops NaN values by default then use Index.tolist to get the list of tuples:
results = df.where(df < .25).stack().index.tolist()
results:
[('A', 'E'), ('C', 'D')]
One option is to reshape the data and filter with the booleans:
df.lt(.25).stack().loc[lambda df: df].index.to_list()
[('A', 'E'), ('C', 'D')]
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