2021年1月6日星期三

How can I combine rows in a pandas dataframe that have similar values in one column to produce the mean of their values?

Apologies for the wording of the question but I can illustrate my problem more clearly below.

Suppose I have the following dataframe:

Region Value
London North 8
North Yorkshire 4
London South 6
South Yorkshire 6

How can I combine the London rows and the Yorkshire rows and produce the mean of their values?

(This is a simplified example of the dataframe I am working with which consists of approx. 3000 rows.)

I would like to get the following dataframe:

Wider Region Avg. Value
London 7
Yorkshire 5
https://stackoverflow.com/questions/65605581/how-can-i-combine-rows-in-a-pandas-dataframe-that-have-similar-values-in-one-col January 07, 2021 at 09:44AM

没有评论:

发表评论