I have a dataframe:
df = c l
A [1,2,3]
A [1,2,3,4]
B [1]
I want to get the mean length (and std) of the column l
, per group of c
.
So here the output will be:
A : 3.5
B : 1
What is the best way to do so?
CodePudding user response:
Use Series.str.len
with aggregate mean
:
s = df['l'].str.len().groupby(df['c']).mean()
print (s)
c
A 3.5
B 1.0
Name: l, dtype: float64
Or with aggregate multiple functions:
df = df['l'].str.len().groupby(df['c']).agg(['mean','std'])
print (df)
mean std
c
A 3.5 0.707107
B 1.0 NaN