Use a ‘groupby‘ in pandas

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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值