I have the following data frame. I wanted to create a new column from the output of pandas groupby().mean()
My code is:
df['AVG_CONSUMED_UNIT'] = df.groupby('transformer')['CONSUMED_UNIT'].transform('mean')
But I found this type of warning:
C:\Users\IGC\AppData\Local\Temp/ipykernel_9332/530264731.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Any idea why this warning occured?
Input:
ID CONSUMED_UNIT transformer
1 100 B/J-80
2 300 B/J-80
3 150 SHK-175
4 120 ACV-17
5 80 ACV-17
Output:
ID CONSUMED_UNIT transformer AVG_CONSUMED_UNIT
1 100 B/J-80 200
2 300 B/J-80 200
3 150 SHK-175 150
4 120 ACV-17 100
5 80 ACV-17 100