I am trying to get a mean value per column per partition for a DataFrame such as this one:
country city sales stock
0 UK London 1 34
1 UK Leeds 2 20
2 UK Leeds 3 21
3 RO Cluj 4 24
4 RO Cluj 5 25
5 RO Bucharest 6 25
That is, I want to get an average of both sales
and stock
, and aggregating these into unique combinations of country
and city
. The resulting DataFrame should therefore be:
country city sales stock
0 UK London 1 34
1 UK Leeds 2.5 20.5
2 RO Cluj 4.5 24.5
3 RO Bucharest 6 25
where duplicate rows of my country-city partition have been aggregated into a single row, with mean values.
I studied the documentation on pandas.DataFrame.mean()
and SO questions & answers such as this one, but none help me in a straightforward manner. Any help appreciated.