Skip to content
Advertisement

Multiplying pandas columns based on multiple conditions

I have a df like this

|  count  | people |  A  |  B  |  C  |
|---------|--------|-----|-----|-----|
|  yes    |  siya  | 4   | 2   |  0  |
|  no     |   aish | 4   | 3   |  0  | 
|  total  |        | 4   |     |  0  | 
|  yes    |   dia  | 6   |  4  |  0  | 
|  no     |   dia  | 6   |   2 |  0  |
|  total  |        | 6   |     |  0  |

I want a output like below

|  count  | people |  A  |  B  |  C  |
|---------|--------|-----|-----|-----|
|  yes    |  siya  | 4   | 2   |  8  |
|  no     |   aish | 4   | 3   |  0  | 
|  total  |        | 4   |     |  0  | 
|  yes    |   dia  | 6   |  4  |  0  | 
|  no     |   dia  | 6   |  2  |  2  |
|  total  |        | 6   |     |  0  |

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

df.C= df.groupby("Host", as_index=False).apply(lambda dfx : df.A * 
                           df.B if (df['count'] == 'no') else df.A *df.B)

But not able to achieve the goal, any idea how can I achieve the output

Advertisement

Answer

import numpy as np

#Set Condtions

c1=df.groupby('people')['count'].transform('nunique').eq(1)&df['count'].eq('yes')
c2=df.groupby('people')['count'].transform('nunique').gt(1)&df['count'].eq('no')
#Put conditions in list
c=[c1,c2]

#Mke choices corresponding to condition list
choice=[df['A']*df['B'],len(df[df['count'].eq('no')])]

#Apply np select
df['C']= np.select(c,choice,0)

print(df)




    count people  A    B    C
0    yes   siya  4  2.0  8.0
1     no   aish  4  3.0  0.0
2  total    NaN  4  0.0  0.0
3    yes    dia  6  4.0  0.0
4     no    dia  6  2.0  2.0
5  total    NaN  6  NaN  0.0
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement