小技巧 20:如何快速拿到数据最多的 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
结果:
大家在看
AI安装教程
AI本地安装教程
微软AI大模型通识教程
微软AI大模型通识教程
AI大模型入门教程
AI大模型入门教程
Python入门教程
Python入门教程
Python进阶教程
Python进阶教程
Python小例子200道练习题
Python小例子200道练习题
Python练手项目
Python练手项目
Python从零在线练习题
Python从零到一60题