Pandas數(shù)據(jù)分析小技巧系列 第三集

三步加星標(biāo)
你好!我是 zhenguo
今天是?Pandas數(shù)據(jù)分析小技巧系列 第三集,涉及如何獲取數(shù)據(jù)最多的3個(gè)分類,以及如何使用count統(tǒng)計(jì)詞條出現(xiàn)次數(shù)。
前兩集在這里:
小技巧 10:如何快速拿到數(shù)據(jù)最多的 3 個(gè)分類?
讀入數(shù)據(jù):
df?=?pd.read_csv("IMDB-Movie-Data.csv")
df

1000 行數(shù)據(jù),genre 取值的頻次統(tǒng)計(jì)如下:
vc?=?df["genre"].value_counts()
vc
打印結(jié)果:
Action,Adventure,Sci-Fi???????50
Drama?????????????????????????48
Comedy,Drama,Romance??????????35
Comedy????????????????????????32
Drama,Romance?????????????????31
??????????????????????????????..
Adventure,Comedy,Fantasy???????1
Biography,History,Thriller?????1
Action,Horror??????????????????1
Mystery,Thriller,Western???????1
Animation,Fantasy??????????????1
Name:?genre,?Length:?207,?dtype:?int64
篩選出 top3 的 index:
top_genre?=?vc[0:3].index
print(top_genre)
打印結(jié)果:
Index(['Action,Adventure,Sci-Fi',?'Drama',?\
???????'Comedy,Drama,Romance'],?dtype='object')
使用得到的 top3 的 index ,結(jié)合 isin,選擇出相應(yīng)的 df
df_top?=?df[df["genre"].isin(top_genre)]
df_top
結(jié)果:

小技巧11:如何使用 count 統(tǒng)計(jì)詞條出現(xiàn)次數(shù)?
讀入 IMDB-Movie-Data 數(shù)據(jù)集,1000行數(shù)據(jù):
df?=?pd.read_csv("../input/imdb-data/IMDB-Movie-Data.csv")
df['Title']
打印 Title 列:
0??????Guardians?of?the?Galaxy
1???????????????????Prometheus
2????????????????????????Split
3?????????????????????????Sing
4????????????????Suicide?Squad
????????????????...
995???????Secret?in?Their?Eyes
996????????????Hostel:?Part?II
997?????Step?Up?2:?The?Streets
998???????????????Search?Party
999?????????????????Nine?Lives
Name:?Title,?Length:?1000,?dtype:?object
標(biāo)題是由幾個(gè)單詞組成,用空格分隔。
df["words_count"]?=?df["Title"].str.count("?")?+?1
df[["Title","words_count"]]

如果你沒有 IMDB-Movie-Data 數(shù)據(jù)集,可以微信聯(lián)系我下載,備注:電影
評(píng)論
圖片
表情
