groupby产生Series和dataframe


groupby 对dataFrame 进行分组,产生的对象是DataFram groupby 对象可以
1.将其转化为list,进行遍历:
2.进行分组和聚合

一、grouby 的使用

import pandas as pd
df=pd.read_csv(r"starbucks_store_worldwide.csv")
grouped=df.groupby(by="Country")
#DataFram groupby 对象可以分组和聚合
# #聚合
#统计美国和中国星巴克的数量
print("方法一:")
country_count=grouped["Brand"].count()#产生的类型是'pandas.core.series.Series'
"""上部中列名(数据完整例如Brand)的进行计数,统计该国家名下的所有星巴克店"""
#下步分别统计US 和CN 国家下的星巴克店
us=country_count["US"]
cn=country_count["CN"]
print(us)
print(cn)
print("方法二:")
#DataFram groupby 对象可以分组和聚合
# #聚合
#统计美国和中国星巴克的数量
"""根据国家名进行统计,因为dataframe是完整的,虽然之前利用country 进行分组,但是只是将datafram进行差分
,根据国家名进行聚合"""
country_count=grouped["Country"].count()
#产生的对象是Series, 根据Country产生的统计值
us_=country_count["US"]
cn_=country_count["CN"]
print(us_)
print(cn_)
######################################################产生结果:
方法一:
13608
2734
方法二:
13608
2734

二、产生dataFrame

区别:

1.Series 的产生:
2.dataframe 的产生:

t1 = df[[“Country”]].groupby(by=[df[“Country”],df[“State/Province”]]).count()t2 = df.groupby(by=[“Country”,“State/Province”])[[“Country”]].count()

以上的两条命令结果一样
和之前的结果的区别在于当前返回的是一个DataFrame类型,增加dataframe中的其他数据

import pandas as pd
df=pd.read_csv(r"starbucks_store_worldwide.csv")
t2=df.groupby(by=["Country","State/Province"])[["Country"]]
t1=df.groupby(by=["Country","State/Province"])["Country"]
t1=list(t1)
t2=list(t2)
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', 10)
print(t1[0:3])
print("*"*100)
print(t2[0:3])

#############################################产生结果:
[(('AD', '7'), 0    AD
Name: Country, dtype: object), (('AE', 'AJ'), 1    AE
2    AE
Name: Country, dtype: object)]
****************************************************************************************************
[(('AD', '7'),        Brand  Store Number     Store Name Ownership Type     Street Address  \
0  Starbucks  47370-257954  Meritxell, 96       Licensed  Av. Meritxell, 96

               City State/Province Country Postcode Phone Number  \
0  Andorra la Vella              7      AD    AD500    376818720

                  Timezone  Longitude  Latitude
0  GMT+1:00 Europe/Andorra       1.53     42.51  ), (('AE', 'AJ'),        Brand  Store Number        Store Name Ownership Type  \
1  Starbucks  22331-212325  Ajman Drive Thru       Licensed
2  Starbucks  47089-256771         Dana Mall       Licensed

                 Street Address   City State/Province Country Postcode  \
1          1 Street 69, Al Jarf  Ajman             AJ      AE      NaN
2  Sheikh Khalifa Bin Zayed St.  Ajman             AJ      AE      NaN

  Phone Number              Timezone  Longitude  Latitude
1          NaN  GMT+04:00 Asia/Dubai      55.47     25.42
2          NaN  GMT+04:00 Asia/Dubai      55.47     25.39  )]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值