I have a df like this
JavaScript
x
9
1
| count | people | A | B | C |
2
|---------|--------|-----|-----|-----|
3
| yes | siya | 4 | 2 | 0 |
4
| no | aish | 4 | 3 | 0 |
5
| total | | 4 | | 0 |
6
| yes | dia | 6 | 4 | 0 |
7
| no | dia | 6 | 2 | 0 |
8
| total | | 6 | | 0 |
9
I want a output like below
JavaScript
1
9
1
| count | people | A | B | C |
2
|---------|--------|-----|-----|-----|
3
| yes | siya | 4 | 2 | 8 |
4
| no | aish | 4 | 3 | 0 |
5
| total | | 4 | | 0 |
6
| yes | dia | 6 | 4 | 0 |
7
| no | dia | 6 | 2 | 2 |
8
| total | | 6 | | 0 |
9
The goal is calculate column C by mulytiplying A and B only when the count value is “yes” but if the column People values are same that is yes for dia and no for also dia , then we have to calculate for the count value “no”
I tried this much so far
JavaScript
1
3
1
df.C= df.groupby("Host", as_index=False).apply(lambda dfx : df.A *
2
df.B if (df['count'] == 'no') else df.A *df.B)
3
But not able to achieve the goal, any idea how can I achieve the output
Advertisement
Answer
JavaScript
1
28
28
1
import numpy as np
2
3
#Set Condtions
4
5
c1=df.groupby('people')['count'].transform('nunique').eq(1)&df['count'].eq('yes')
6
c2=df.groupby('people')['count'].transform('nunique').gt(1)&df['count'].eq('no')
7
#Put conditions in list
8
c=[c1,c2]
9
10
#Mke choices corresponding to condition list
11
choice=[df['A']*df['B'],len(df[df['count'].eq('no')])]
12
13
#Apply np select
14
df['C']= np.select(c,choice,0)
15
16
print(df)
17
18
19
20
21
count people A B C
22
0 yes siya 4 2.0 8.0
23
1 no aish 4 3.0 0.0
24
2 total NaN 4 0.0 0.0
25
3 yes dia 6 4.0 0.0
26
4 no dia 6 2.0 2.0
27
5 total NaN 6 NaN 0.0
28