简体   繁体   中英

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. If Col3 has same values for all corresponding values in Col1/Col2, Col4 should read as "YES" otherwise "NO".

[ 数据 ]

[ 预期结果_Col4 ]

Use GroupBy.transform with count number of unique values and compare by 1 , set new values by numpy.where :

mask = df.groupby(['Col1','Col2'])['Col3'].transform('nunique') == 1
df['Col4'] = np.where(mask, 'yes', 'no')

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