Home > Mobile >  Pandas return index and column name based on condition
Pandas return index and column name based on condition

Time:11-05

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')]

CodePudding user response:

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')]

CodePudding user response:

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')]
  • Related