Python – Pandas GroupBy mean

Pandas GroupBy mean… here is a solution to the problem.

Pandas GroupBy mean

The following code works for the total number of each column, but I want to calculate the average for each species.

# Read data file into array
data = numpy.genfromtxt('data/iris.csv', delimiter=',')
# picking the first column of data
firstcol = data[:,0]
meanfirstcol = numpy.mean(data[:,0]) #defining meanfirstcol
print("Mean of First Column is:", meanfirstcol)

Solution

For pandas, it’s easy. You only need to do groupby – on the species column

import seaborn as sns
df = sns.load_dataset('iris')

df.groupby('species').mean()
            sepal_length  sepal_width  petal_length  petal_width
species                                                         
setosa             5.006        3.428         1.462        0.246
versicolor         5.936        2.770         4.260        1.326
virginica          6.588        2.974         5.552        2.026

Related Problems and Solutions