日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

要求:比較美國和中國星巴克店鋪數(shù)量

數(shù)據(jù)來源:
https://www.kaggle.com/starbucks/store-locations/data

在pandas中類似的分組的操作我們有很簡單的方式來完成

df.groupby(by="columns_name")

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)

結(jié)果返回的是一個DataFrameGroupBy

數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

 

那么問題來了,DataFrameGroupBy是什么內(nèi)容?

它可以完成遍歷,分組操作

1.遍歷grouped的結(jié)果,他返回的是一個列表每個國家有關(guān)星巴克的情況

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)

for i in  grouped:
    print(i)
數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

 

擴展:如果想單獨把第一個元素(國家)打印出來

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)

for i,j in  grouped:
    print(i)
    print('*'*100)
    print(j)
數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

 

2.分組

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)


b=grouped.count()
print(b)
數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

 

但我們只想要Brand的數(shù)據(jù)

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)

#聚合方法
b=grouped["Brand"].count()
print(b)
數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

 

我們最終想要US和CN的值

import pandas as pd

#獲取數(shù)據(jù)
filepath="./starbucks_store_worldwide.csv"
df=pd.read_csv(filepath)

grouped = df.groupby(by="Country")
print(grouped)

b=grouped["Brand"].count()
c=b['US']
d=b['CN']
print(c)
print(d)
數(shù)據(jù)分析-pandas之DataFrame實例數(shù)據(jù)分組聚合1

分享到:
標簽:分析 數(shù)據(jù)
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定