Grouping and Transforming in pandas
Use GroupBy.transform
for return Series
with same size like original DataFrame
, so possible create new column.
df['New'] = df.groupby('id').Name.transform(lambda x: x.value_counts().index[0])
Another solution:
df['New'] = df.groupby('id').Name.transform(lambda x: x.mode().iat[0])
print (df) Name Value id New0 Alegro 0.850122 alegro ALEGRO1 Alegro 0.447362 alegro ALEGRO2 AlEgro 0.711295 alegro ALEGRO3 ALEGRO 0.123761 alegro ALEGRO4 alegRo 0.273111 alegro ALEGRO5 ALEGRO 0.564893 alegro ALEGRO6 ALEGRO 0.276369 alegro ALEGRO7 ALEGRO 0.526434 alegro ALEGRO8 ALEGRO 0.924014 alegro ALEGRO9 ALEGrO 0.629207 alegro ALEGRO10 Belagio 0.834231 belagio Belagio11 BElagio 0.788357 belagio Belagio12 Belagio 0.092156 belagio Belagio13 BeLaGio 0.810275 belagio Belagio