rafine
rafine

Reputation: 471

pandas groupby - group names instead of numbers

Groupby.

In my gender parameter. The values are numeric .

1- male 2- female

Can I change (just for the output!) the values to names?

df.groupby('gender')['age'].mean()

Out[765]:

gender

1 21.166667

2 17.500000

Name: age, dtype: float64

Upvotes: 1

Views: 413

Answers (1)

abc
abc

Reputation: 11929

You could use the series.rename method.

df.groupby('gender')['age'].mean().rename(index={1:"male", 2:"female"})

Upvotes: 1

Related Questions