简体   繁体   中英

create a new dataframes with rows from another dataframe with specific columns values

I would like to create a new pandas dataframe from an existing dataframe with only rows where specific columns values are bigger than x. For example:

df =   A B C
     0 1 0 2
     1 2 1 3
     2 3 3 0
     3 4 2 5

I want to create new_df with only the rows where the values of columns B and C are bigger than 0 which means

new_df =  A B C
        0 2 1 3
        1 4 2 5

I used the command:

new_df = df.loc[df['B'] > 0 & df['C'] > 0]

but it doesn't work

You almost had it. Use parenthesis to enclose conditions:

new_df = df.loc[(df['B'] > 0) & (df['C'] > 0)]

The binary operators & and | take precedence over equalities/inequalities. So what you were doing was equivalent to:

new_df = df.loc[(df['B'] > (0 & df['C'])) > 0]

You can use this:

df[(df[['B', 'C']] > 0).all(axis=1)]

Output:

   A  B  C
1  2  1  3
3  4  2  5

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