Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96360

Identify DataFrame index object where a condition is met

How do I get an index object from my DataFrame when a specific condition is met, say for a given column?

The following returns a Series object with True/False values where some condition in the foo column of my_dataframe is met:

true_entries = my_dataframe['foo'].apply(my_lambda_function) == True

But what I would like to get is an index object corresponding to those entries.

How do I do this?

As an example of an application, I would like to drop rows in my dataframe where that condition is met.

Update:

I tried the suggestion from @DSM to drop entries (rows) from my dataframe, but the following command:

indices_to_drop = my_df.index[my_df['foo'].apply(my_lambda_function) == True]
my_df.drop(indices_to_drop)

returns:

DeprecationWarning: height has been deprecated

Upvotes: 1

Views: 1724

Answers (1)

DSM
DSM

Reputation: 353379

If you want an Index corresponding to the true values, you can use the boolean Series as a slice on df.index:

>>> df
    A  B
0  10 -5
1  20 -2
2  30  1
3  40  4
4  50  7
>>> df["B"].apply(lambda x: x % 2 == 0)
0    False
1     True
2    False
3     True
4    False
Name: B, dtype: bool
>>> df.index[df["B"].apply(lambda x: x % 2 == 0)]
Int64Index([1, 3], dtype=int64)

But if you only want to get rid of rows where that condition is met, you don't need to construct an explicit index at all. You can simply keep the ones where the condition isn't met:

>>> df[~df["B"].apply(lambda x: x % 2 == 0)]
    A  B
0  10 -5
2  30  1
4  50  7

Upvotes: 2

Related Questions