机器学习-数据科学库(第五天)
31.數(shù)據(jù)的合并和分組聚合——字符串離散化的案例
字符串離散化的案例
剛剛我們學(xué)會了數(shù)據(jù)分合并,那么接下來,我們按照電影分類(genre)信息把數(shù)據(jù)呈現(xiàn)出來
import numpy as np import pandas as pd from matplotlib import pyplot as plt file_path = "/Users/zhucan/Desktop/IMDB-Movie-Data.csv" df = pd.read_csv(file_path)temp_list = df["Genre"].str.split(",").tolist() genre_list = list(set([i for j in temp_list for i in j]))#構(gòu)造全為0的數(shù)組 zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns=genre_list) print(zeros_df)#給每個電影出現(xiàn)分類的位置賦值1 for i in range(df.shape[0]):zeros_df.loc[i,temp_list[i]] = 1#統(tǒng)計(jì)每個分類的電影的數(shù)量和 genre_count = zeros_df.sum(axis=0) print(genre_count)genre_count = genre_count.sort_values() _x = genre_count.index _y = genre_count.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y) plt.xticks(range(len(_x)),_x) plt.show()32.數(shù)據(jù)的合并和分組聚合——數(shù)據(jù)合并
數(shù)據(jù)合并之join
數(shù)據(jù)合并之merge?
merge:按照指定的列把數(shù)據(jù)按照一定的方式合并到一起?
默認(rèn)的合并方式inner,交集?
merge outer,并集,NaN補(bǔ)全
merge left,左邊為準(zhǔn),NaN補(bǔ)全。merge right,右邊為準(zhǔn),NaN補(bǔ)全?
33.數(shù)據(jù)分組聚合
數(shù)據(jù)分組聚合
現(xiàn)在我們有一組關(guān)于全球星巴克店鋪的統(tǒng)計(jì)數(shù)據(jù),如果我想知道美國的星巴克數(shù)量和中國的哪個多,或者我想知道中國每個省份星巴克的數(shù)量的情況,那么應(yīng)該怎么辦?
import pandas as pd import numpy as np file_path = "/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path) grouped = df.groupby(by="Country")country_count = grouped["Brand"].count() print(country_count["US"]) print(country_count["CN"]) 13608 273434.數(shù)據(jù)分組聚合02
數(shù)據(jù)分組聚合02
import pandas as pd import numpy as np file_path = "/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path)china_data = df[df["Country"] == "CN"] grouped = china_data.groupby(by = "State/Province").count()["Brand"] print(grouped)分組和聚合
多條件分組?
如果我們需要對國家和省份進(jìn)行分組統(tǒng)計(jì),應(yīng)該怎么操作呢?
grouped = df.groupby(by=[df["Country"],df["State/Province"]])
很多時候我們只希望對獲取分組之后的某一部分?jǐn)?shù)據(jù),或者說我們只希望對某幾列數(shù)據(jù)進(jìn)行分組,這個時候我們應(yīng)該怎么辦呢? 獲取分組之后的某一部分?jǐn)?shù)據(jù): ?
df.groupby(by=["Country","State/Province"])["Country"].count()
對某幾列數(shù)據(jù)進(jìn)行分組: ?
df["Country"].groupby(by=[df["Country"],df["State/Province"]]).count()?
觀察結(jié)果,由于只選擇了一列數(shù)據(jù),所以結(jié)果是一個Series類型 如果我想返回一個DataFrame類型呢?
df[["Country"]].groupby(by=[df["Country"],df["State/Province"]]).count()?
35.數(shù)據(jù)的索引學(xué)習(xí)
索引和復(fù)合索引
36.數(shù)據(jù)分組聚合練習(xí)和總結(jié)
Series復(fù)合索引
DataFrame復(fù)合索引
使用matplotlib呈現(xiàn)出店鋪總數(shù)排名前10的國家
import matplotlib.pyplot as plt import pandas as pd import numpy as np file_path ="/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path)data1 = df.groupby(by="Country").count()["Brand"].sort_values(ascending=False)[:10] _x = data1.index _y = data1.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y) plt.xticks(range(len(_x)),_x) plt.show()使用matplotlib呈現(xiàn)出每個中國每個城市的店鋪數(shù)量?
import matplotlib.pyplot as plt import pandas as pd import numpy as np from matplotlib import font_manager file_path ="/Users/zhucan/Desktop/starbucks_store_worldwide.csv" my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc") df = pd.read_csv(file_path) df = df[df["Country"]=="CN"]data1 = df.groupby(by="City").count()["Brand"].sort_values(ascending=False)[:25]_x = data1.index _y = data1.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y,width=0.3,color="orange") plt.xticks(range(len(_x)),_x,fontproperties=my_font) plt.show()現(xiàn)在我們有全球排名靠前的10000本書的數(shù)據(jù),那么請統(tǒng)計(jì)一下下面幾個問題:
總結(jié)
以上是生活随笔為你收集整理的机器学习-数据科学库(第五天)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习-数据科学库(第四天)
- 下一篇: 机器学习-数据科学库(第六天)