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
CodePudding user response:
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 |
CodePudding user response:
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