Suji
Suji

Reputation: 87

add calculated column using pandas

data

How can I add a calculated column (Col4) using pandas based on the values in Col1,2,3. Column 4 value to be "yes" only is all value in Col3 are yes for corresping values in Col1,2.

Upvotes: 1

Views: 83

Answers (2)

ansev
ansev

Reputation: 30920

Use:

df['Col4']=df.groupby(['Col1','Col2']).Col3.transform(lambda x: 'yes' if x.eq('yes').all() else 'no')
print(df)

   Col1 Col2 Col3 Col4
0     1  aaa  yes   no
1     1  aaa   no   no
2     1  aaa  yes   no
3     4  bbb  yes   no
4     4  bbb   no   no
5     4  bbb  yes   no
6     7  ccc  yes  yes
7     7  ccc  yes  yes

Upvotes: 1

BENY
BENY

Reputation: 323226

We can use transform

df['Col4']=df.Col3.eq('Yes').groupby([df.Col1,df.Col2]).transform('all').\
                  map({True:'Yes',False: 'No'})

Upvotes: 4

Related Questions