I'd like to merge rows in my data based on averaging select rows in one column. I would like all the repeated countries and their associated rates to be merged so that there is one occurrence of the country in the country column, and an average rate in the average column.
From this data frame:
country rate
Albania 1.4
Albania 2.3
Albania 1.8
Bulgaria 3.5
Bulgaria 2.8
Croatia 1.8
Croatia 1.3
Croatia 2.1
Croatia 2.3
Denmark 5.6
Denmark 5.7
To this new dataframe:
country average_rate
Albania 1.83
Bulgaria 3.15
Croatia 1.875
Denmark 6.65
There is a total of 30 countries, and each country has a varying number of samples for each between 4-20.
Is it possible to write a code without having to specifically go to each country and count how many occurrences there are and manually calculate?