Thomas Johnson
Thomas Johnson

Reputation: 11658

How can I select series in a pandas dataframe where every element in the series meets a criterion?

I'd like to get all the series in my dataframe where every element in the series passes a check (for example, every value is less than 0).

I tried:

dataframe[dataframe<=0]

and that seems to give me all the elements of each series that are less than zero.

Then I tried

dataframe[dataframe<=0].dropna()

to try to get rid of the series that have invalid values, but that gives me...something that I don't really understand.

How can I get all the elements in a dataframe that are less than zero (or more generally, all elements that meet a condition)?

Upvotes: 1

Views: 487

Answers (1)

EdChum
EdChum

Reputation: 393973

You need to set axis=1 as a param to dropna in order for it to consider whether it is dealing row-wise or column-wise as the criteria for dropping values. By default it is row-wise.

dataframe[dataframe<=0].dropna(axis=1)

Check the online docs for dropna and the overall section on working with missing data to see other ways of dealing with NaN values.

Upvotes: 1

Related Questions