pandas10_高级处理_分组与聚合

import numpy as np
import pandas as pd

分组与聚合

  • DataFrame.groupby(key, as_index=False)
  • as_index=False添加一列索引,默认为true
  • key:分组的列数据,可以多个
# 案例:不同颜色的不同笔的价格数据
col =pd.DataFrame({'color': ['white','red','green','red','green'], 'object': ['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.30,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]})
col
colorobjectprice1price2
0whitepen5.564.75
1redpencil4.204.12
2greenpencil1.301.60
3redashtray0.560.75
4greenpen2.753.15
  • 进行分组,对颜色分组,price进行聚合
# 分组,求平均值
col.groupby(['color'])['price1'].mean()
color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64
# 分组,数据的结构不变
col.groupby(['color'],as_index=False)['price1'].mean()
color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64

实例:星巴克零售店铺数据

  • 如果我想知道美国的星巴克数量和中国的哪个多

数据获取

# 导入星巴克店的数据
starbucks = pd.read_csv("./data/starbucks/directory.csv")
starbucks.head()
BrandStore NumberStore NameOwnership TypeStreet AddressCityState/ProvinceCountryPostcodePhone NumberTimezoneLongitudeLatitude
0Starbucks47370-257954Meritxell, 96LicensedAv. Meritxell, 96Andorra la Vella7ADAD500376818720GMT+1:00 Europe/Andorra1.5342.51
1Starbucks22331-212325Ajman Drive ThruLicensed1 Street 69, Al JarfAjmanAJAENaNNaNGMT+04:00 Asia/Dubai55.4725.42
2Starbucks47089-256771Dana MallLicensedSheikh Khalifa Bin Zayed St.AjmanAJAENaNNaNGMT+04:00 Asia/Dubai55.4725.39
3Starbucks22126-218024Twofour 54LicensedAl Salam StreetAbu DhabiAZAENaNNaNGMT+04:00 Asia/Dubai54.3824.48
4Starbucks17127-178586Al Ain TowerLicensedKhaldiya Area, Abu Dhabi IslandAbu DhabiAZAENaNNaNGMT+04:00 Asia/Dubai54.5424.51

进行分组聚合

count = starbucks.groupby(['Country']).count()
count
BrandStore NumberStore NameOwnership TypeStreet AddressCityState/ProvincePostcodePhone NumberTimezoneLongitudeLatitude
Country
AD111111111111
AE1441441441441441441442478144144144
AR10810810810810810810810029108108108
AT181818181818181817181818
AU22222222222222220222222
.......................................
TT333333330333
TW39439439439439439439436539394394394
US136081360813608136081360813608136081360713122136081360813608
VN252525252525252523252525
ZA333333332333

73 rows × 12 columns

画图显示结果

count['Brand'].plot(kind='bar', figsize=(20, 8))

在这里插入图片描述

count1 = count['Brand'].sort_values(ascending=False).head(10)
count1
Country
US    13608
CN     2734
CA     1468
JP     1237
KR      993
GB      901
MX      579
TW      394
TR      326
PH      298
Name: Brand, dtype: int64
count1.plot(kind='bar', figsize=(8, 5))

在这里插入图片描述

  • 想知道中国每个省份星巴克的数量的情况
data1 = starbucks.groupby(['Country', 'State/Province']).count()
data1
BrandStore NumberStore NameOwnership TypeStreet AddressCityPostcodePhone NumberTimezoneLongitudeLatitude
CountryState/Province
AD711111111111
AEAJ22222200222
AZ484848484848720484848
DU8282828282821650828282
FU22222210222
.......................................
USWV2525252525252523252525
WY2323232323232322232323
VNHN66666666666
SG1919191919191917191919
ZAGT33333332333

545 rows × 11 columns

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GJ_WL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值