简体   繁体   中英

add calculated column using pandas

数据

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.

We can use transform

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

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

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