bsky
bsky

Reputation: 20242

Get column numbers of dataframe where given condition holds

I have the following code:

raw_data = [[1, 2, 3], [4, 5, 6]]
df = pd.DataFrame(data=raw_data,
                  columns=["cA", "cB", "cC"])

wrong_indexes = df.loc[df['cA'] > 2 ]
print(wrong_indexes)

This prints:

   cA  cB  cC
1   4   5   6

Instead of this, I would like to only get a list of indexes at which this condition holds, like so:

[1]

Any idea how I can do that?

Upvotes: 1

Views: 32

Answers (1)

Gabriel A
Gabriel A

Reputation: 1827

wrong_indexes = df.loc[df['cA'] > 2 ].index.tolist()

Upvotes: 2

Related Questions