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".
[ ]
[ ]
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.