简体   繁体   中英

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM