简体   繁体   中英

Count number of columns that meet condition

For each row in df, I want to count how many value belonging to column AA and B greater than 2 and save this count to a new column in the same df. So in the df below, row 0, the count =1, in row 1, count=2. Any help please?

import numpy as np
import pandas as pd
df = pd.DataFrame({
        "AA": [3,6,0,3,4],
        "B": [1,5,1,1,4],
        "W": [1,2,1,1,9]
    })
df

You can set axis=1 when using apply() to work with cells from a single row, but many columns.

df['count'] = df.apply(lambda x: sum(item > 2 for item in[x['AA'], x['B']]), axis=1)

df
index AA B W count
0 3 1 1 1
1 6 5 2 2
2 0 1 1 0
3 3 1 1 1
4 4 4 9 2

Let's try

df['count'] = df[['AA', 'B']].gt(2).sum(axis=1)
print(df)


   AA  B  W  count
0   3  1  1      1
1   6  5  2      2
2   0  1  1      0
3   3  1  1      1
4   4  4  9      2

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