统计123出现次数_如何使用 count 统计词条出现次数?
如何獲取數據最多的3個分類?如何使用count統計詞條出現次數?今天番茄加速跟大家講一下。
如何快速拿到數據最多的 3 個分類?
讀入數據:
df = pd.read_csv("IMDB-Movie-Data.csv")
df
1000 行數據,genre 取值的頻次統計如下:
vc = df["genre"].value_counts()
vc
打印結果:
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)
打印結果:
Index(['Action,Adventure,Sci-Fi', 'Drama',
'Comedy,Drama,Romance'], dtype='object')
使用得到的 top3 的 index ,結合 isin,選擇出相應的 df
df_top = df[df["genre"].isin(top_genre)]
df_top
結果:
如何使用 count 統計詞條出現次數?
讀入 IMDB-Movie-Data 數據集,1000行數據:
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
標題是由幾個單詞組成,用空格分隔。
df["words_count"] = df["Title"].str.count(" ") + 1
df[["Title","words_count"]]
總結
以上是生活随笔為你收集整理的统计123出现次数_如何使用 count 统计词条出现次数?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长按发送语音_MIUI 11指尖通话功能
- 下一篇: vue 获取当前位置 高德_vue高德地