0

I have a dataframe looks like this:

df
Speed   Zone
1.33    Zone 1
0.37    Zone 1
0.52    Zone 1
1.17    Zone 1
8.36    Zone 2
4.46    Zone 2
2.16    Zone 2
4.45    Zone 2
5.50    Zone 3
5.29    Zone 3
3.49    Zone 3
1.11    Zone 3
0.89    Zone 4
2.16    Zone 5
0.83    Zone 5
1.17    Zone 5

I calculate average or mean of the speed every zone by using this code:

import geopandas as gpd
import pandas as pd
import numpy as np
df = pd.read_csv("speed_zone.csv")
df = df[df.Zone == 'Zone 1']
df["Speed"].mean()

However, I have to copy and do it again into a new cell. I have many zones to do. I am a beginner in python, how to calculate mean or average of the speed column and make a table automatically simultaneous. My expected result looks like this:

Mean_Speed  Zone 
0.8475      Zone 1
4.8575      Zone 2
3.8474      Zone 3
Arief Hidayat
  • 937
  • 1
  • 8
  • 19

1 Answers1

1

Did you try:

df.groupby("Zone").agg("mean")

You might also want to look at the documentation of agg. You can specify different aggregations for each variable

Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42