I'm working on Covid data from
Here's what I tried:
import time
t = time.process_time()
avg_life_exp = {}
for i in covid['location']:
count = 0
avg = 0
loc = i
if i == loc:
avg = covid['life_expectancy']
count = 1
else:
avg = avg/count
avg_life_exp[i] = avg
print(avg_life_exp)
elapsed_time = time.process_time() - t
print(elapsed_time)
Output (I got repeated values of the added screenshot):
CodePudding user response:
Use groupby
and compute mean
on life_expectancy,
:
out = df.groupby('location')['life_expectancy'].mean().reset_index()
print(out)
# Output
location life_expectancy
0 Afghanistan 64.83
1 Africa NaN
2 Albania 78.57
3 Algeria 76.88
4 Andorra 83.73
.. ... ...
233 Wallis and Futuna 79.94
234 World 72.58
235 Yemen 66.12
236 Zambia 63.89
237 Zimbabwe 61.49
[238 rows x 2 columns]