3

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')]
2
  • 2
    try : A.lt(.25).stack().loc[lambda df: df].index.to_list() Commented Nov 4, 2021 at 23:32
  • @DHJ Please do not edit your question to include the answer (Questions and Answers should be separate on SO). Just notify the commenter and ask them if they would like to post that as an answer like I did. Commented Nov 4, 2021 at 23:54

2 Answers 2

4

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')]
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.