我有一个数据帧,有两列:流派和发行年份。每年都有多种流派。格式如下:
genre release_year
Action 2015
Action 2015
Adventure 2015
Action 2015
Action 2015
我需要用Pandas/Python绘制这些年来所有流派的变化。在
^{pr2}$这将导致以下分组。在
release_year genre
1960 Action 8
Adventure 5
Comedy 8
Crime 2
Drama 13
Family 3
Fantasy 2
Foreign 1
History 5
Horror 7
Music 1
Romance 6
Science Fiction 3
Thriller 6
War 2
Western 6
1961 Action 7
Adventure 6
Animation 1
Comedy 10
Crime 2
Drama 16
Family 5
Fantasy 2
Foreign 1
History 3
Horror 3
Music 2
Mystery 1
Romance 7
...
我需要用线条图来描绘这些年来体裁特征的变化。i、 我必须有一个循环来帮助我在这些年里为每一种类型设计。例如
df_action = df.query('genre == "Action"')
result_plot = df_action.groupby(['release_year','genre'])['genre'].count()
result_plot.plot(figsize=(10,10));
显示类型“动作”的情节。同样的,我需要一个循环,而不是每一个类型。在
我怎么能做到呢?有人能帮我吗?在
我试过下面的方法,但没用。在
genres = ["Action", "Adventure", "Western", "Science Fiction", "Drama",
"Family", "Comedy", "Crime", "Romance", "War", "Mystery",
"Thriller", "Fantasy", "History", "Animation", "Horror", "Music",
"Documentary", "TV Movie", "Foreign"]
for g in genres:
#df_new = df.query('genre == "g"')
result_plot = df.groupby(['release_year','genre'])['genre'].count()
result_plot.plot(figsize=(10,10));
我选择了条形图,你可以改成你需要的what ever
PS:您可以考虑}
^{pr2}$crosstab
,而不是{把你的序列拆开,用一个命令把所有的东西都画出来怎么样:
绘图:
^{pr2}${I>建议在绘图前使用框架的操作。您可以通过运行
pip install seaborn
来安装它。对于标准类型的绘图,它有一个简单的API:发行年份vs流派
流派vs发行年份
^{pr2}$相关问题 更多 >
编程相关推荐