How to calculate mean value and appending it to a column? #python
Thank you for your help Ivan!
To answer your Qs.

Yes, I'm trying to calculate the mean values of the latitude & the longitude.

I'll try to explained it better. The shape of the original df I deal with is (15, 30000).
It contians electric vehicles charging records in a City.



Each ID refer to to a contract ID / User.
some users charge regularly, mostly at the same charging station or other close-by stations.
I'm trying to Filter the charge records by Each ID ans calculate the mean lon & lat at which charge events happened.
This mean values of geocoordinates indicates where the User lives (roughly assumptions).





ID
LON
LAT
Station
STARTTIME
ENDTIME




A
62.03755
16.34481
stationname
timestamp
timestamp


B
-50.37181
54.94410
stationname
timestamp
timestamp


C
16.95291
50.35189
stationname
timestamp
timestamp


B
59.95044
173.64574
stationname
timestamp
timestamp


A
31.31972
-128.33218
stationname
timestamp
timestamp


B
-50.37181
54.94410
stationname
timestamp
timestamp


A
23.11042
157.43303
stationname
timestamp
timestamp




What I'm trying to get is like:




ID
LON
LAT
Station
STARTTIME
ENDTIME




A
Mean of LON values for A
Mean of LAT values for A
stationname
timestamp
timestamp


B
Mean of LON values for B
Mean of LAT values for B
stationname
timestamp
timestamp


C
Mean of LON vakues for C
Mean of LAT values for C
stationname
timestamp
timestamp




Or




ID
LON
LAT
Station
STARTTIME
ENDTIME
LONMEAN
LATMEAN




A
62.03755
16.34481
stationname
timestamp
timestamp
Mean of LON values for A
Mean of LAT values for A


B
-50.37181
54.94410
stationname
timestamp
timestamp
Mean of LON values for B
Mean of LAT values for B


C
16.95291
50.35189
stationname
timestamp
timestamp
Mean of LON vakues for C
Mean of LAT values for C


B
59.95044
173.64574
stationname
timestamp
timestamp
Mean of LON values for B
Mean of LAT values for B


A
31.31972
-128.33218
stationname
timestamp
timestamp
Mean of LON values for A
Mean of LAT values for A


B
-50.37181
54.94410
stationname
timestamp
timestamp
Mean of LON values for B
Mean of LAT values for B


A
23.11042
157.43303
stationname
timestamp
timestamp
Mean of LON values for A
Mean of LAT values for A
I have an example of a dataset (below). Column ['ID'] has values refering to customer codes A, B, C. Each customer-code has been to different locations (referred to as ['LON'] and ['LAT'].
I am trying to group each ID and calculate the mean value of the corresponding LON and LAT values. After the calculation, I try to append the mean value in the same column or a new column but it doesn't seem to work (runs into an error that the column isn't defined).
Could you please shed some light?
Thanks so much!




ID
LON
LAT




A
62.03755
16.34481


B
-50.37181
54.94410


C
16.95291
50.35189


B
59.95044
173.64574


A
31.31972
-128.33218


B
-50.37181
54.94410


A
23.11042
157.43303


B
2.15615
97.10632




I tried this:
df.groupby('ID')['LON'].mean().append
and
df['MEANLON'] = df.groupby('ID', as_index=False)['LON'].mean()