Use a ‘groupby’ in pandas
import pandas as pd
drinks = pd.read_csv('drinks.csv')
drinks
country | beer_servings | spirit_servings | wine_servings | total_litres_of_pure_alcohol | continent | |
---|---|---|---|---|---|---|
0 | Afghanistan | 0 | 0 | 0 | 0.0 | Asia |
1 | Albania | 89 | 132 | 54 | 4.9 | Europe |
2 | Algeria | 25 | 0 | 14 | 0.7 | Africa |
3 | Andorra | 245 | 138 | 312 | 12.4 | Europe |
4 | Angola | 217 | 57 | 45 | 5.9 | Africa |
... | ... | ... | ... | ... | ... | ... |
188 | Venezuela | 333 | 100 | 3 | 7.7 | South America |
189 | Vietnam | 111 | 2 | 1 | 2.0 | Asia |
190 | Yemen | 6 | 0 | 0 | 0.1 | Asia |
191 | Zambia | 32 | 19 | 4 | 2.5 | Africa |
192 | Zimbabwe | 64 | 18 | 4 | 4.7 | Africa |
193 rows × 6 columns
drinks.beer_servings.mean()
115.2
Core code is here!
drinks.groupby('continent')
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000015AFF69A408>
print(type(drinks.groupby('continent')))
<class 'pandas.core.groupby.generic.DataFrameGroupBy'>
drinks.groupby('continent').beer_servings
<pandas.core.groupby.generic.SeriesGroupBy object at 0x0000015AFF69F7C8>
print(type(drinks.groupby('continent').beer_servings))
<class 'pandas.core.groupby.generic.SeriesGroupBy'>
drinks.groupby("continent"