Python:按多行值分组打印

2024-06-16 10:55:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,有两列:流派和发行年份。每年都有多种流派。格式如下:

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));

Tags: dfreleaseplotactionresultfamilyyearfantasy
3条回答
df_new.unstack().T.plot(kind='bar')

我选择了条形图,你可以改成你需要的what ever

PS:您可以考虑crosstab,而不是{}

^{pr2}$

enter image description here

把你的序列拆开,用一个命令把所有的东西都画出来怎么样:

In [36]: s
Out[36]:
release_year  genre
1960.0        Action        8
              Adventure     5
              Comedy        8
              Crime         2
              Drama        13
              Family        3
              Fantasy       2
              Foreign       1
              History       5
              Horror        7
                           ..
1961.0        Crime         2
              Drama        16
              Family        5
              Fantasy       2
              Foreign       1
              History       3
              Horror        3
              Music         2
              Mystery       1
              Romance       7
Name: count, Length: 30, dtype: int64

In [37]: s.unstack()
Out[37]:
genre         Action  Adventure  Animation  Comedy  Crime  Drama  Family  Fantasy  Foreign  History  Horror  Music  Mystery  Romance  \
release_year
1960.0           8.0        5.0        NaN     8.0    2.0   13.0     3.0      2.0      1.0      5.0     7.0    1.0      NaN      6.0
1961.0           7.0        6.0        1.0    10.0    2.0   16.0     5.0      2.0      1.0      3.0     3.0    2.0      1.0      7.0

genre         Science Fiction  Thriller  War  Western
release_year
1960.0                    3.0       6.0  2.0      6.0
1961.0                    NaN       NaN  NaN      NaN

绘图:

^{pr2}$

{I>建议在绘图前使用框架的操作。您可以通过运行pip install seaborn来安装它。对于标准类型的绘图,它有一个简单的API:

发行年份vs流派

import seaborn as sns
sns.countplot(x='release_year', hue='genre', data=df)

release_year vs genre

流派vs发行年份

^{pr2}$

genre vs release_year

相关问题 更多 >