[英]add calculated column using pandas based on values in other column
I need help with adding Col4 based on data from Col1,2 and Col3.我需要根据 Col1,2 和 Col3 的数据添加 Col4 的帮助。 If Col3 has same values for all corresponding values in Col1/Col2, Col4 should read as "YES" otherwise "NO".如果 Col3 对 Col1/Col2 中的所有对应值具有相同的值,则 Col4 应读为“YES”,否则读为“NO”。
[ [ ] ]
[ [ ] ]
Use GroupBy.transform
with count number of unique values and compare by 1
, set new values by numpy.where
:使用GroupBy.transform
计数唯一值并按1
进行比较,通过numpy.where
设置新值:
mask = df.groupby(['Col1','Col2'])['Col3'].transform('nunique') == 1
df['Col4'] = np.where(mask, 'yes', 'no')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.