回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个数据帧,有两列:流派和发行年份。每年都有多种流派。格式如下:</p>
<pre><code>genre release_year
Action 2015
Action 2015
Adventure 2015
Action 2015
Action 2015
</code></pre>
<p>我需要用Pandas/Python绘制这些年来所有流派的变化。在</p>
^{pr2}$
<p>这将导致以下分组。在</p>
<pre><code>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
...
</code></pre>
<p>我需要用线条图来描绘这些年来体裁特征的变化。i、 我必须有一个循环来帮助我在这些年里为每一种类型设计。例如</p>
<pre><code>df_action = df.query('genre == "Action"')
result_plot = df_action.groupby(['release_year','genre'])['genre'].count()
result_plot.plot(figsize=(10,10));
</code></pre>
<p>显示类型“动作”的情节。同样的,我需要一个循环,而不是每一个类型。在</p>
<p>我怎么能做到呢?有人能帮我吗?在</p>
<p>我试过下面的方法,但没用。在</p>
<pre><code>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));
</code></pre>