我有一个数据帧,它有2列:genre和release_year。每年都有多种流派。格式如下:Python:按多列分组的值线图
genre release_year
Action 2015
Action 2015
Adventure 2015
Action 2015
Action 2015
我需要使用Pandas/Python绘制所有类型的变化。
df = pd.read('genres.csv')
df.shape
(53975, 2)
df_new = df.groupby(['release_year', 'genre'])['genre'].count()
这会导致以下分组。
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
...
我需要为多年来流派特征的变化绘制线图。即我必须有一个循环,这可以帮助我绘制多年来的各种流派。例如,
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));