简体   繁体   English

根据其他列中的值使用 pandas 添加计算列

[英]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”。

[ [数据 ] ]

[ [预期结果_Col4 ] ]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM