Replace Values By The Mean
I have a dataframe as follow: Col1 Price 1 Plastic 50 2 Metal 100 3 Plastic 40 I would like to replace values in Col1 by thei
Solution 1:
You are correct- map
can be used to do this like so:
df['Col1'] = df['Col1'].map(df.groupby('Col1')['Price'].mean())
df
Col1 Price
1 45 50
2 100 100
3 45 40
Solution 2:
df.assign(Col1=df.Col1.map(df.groupby('Col1').mean().squeeze()))
Output:
Col1 Price
1 45 50
2 100 100
3 45 40
Post a Comment for "Replace Values By The Mean"