【问题标题】:Python: line plot for values grouped by multiple columnsPython:按多列分组的值的线图
【发布时间】:2017-12-27 20:06:33
【问题描述】:

我有一个包含 2 列的数据框:genre 和 release_year。每年都有多种流派。格式如下:

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

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    如何解开你的系列并在一个命令中绘制所有内容:

    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
    

    绘图:

    s.unstack().plot()
    

    【讨论】:

      【解决方案2】:
      df_new.unstack().T.plot(kind='bar')
      

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

      PS:你可以考虑crosstab而不是groupby

      pd.crosstab(df.genre,df.release_year).plot(kind='bar')
      

      【讨论】:

        【解决方案3】:

        我建议使用seaborn,这将有助于避免在绘图前操纵数据框。您可以通过运行pip install seaborn 来安装它。它有一个用于标准绘图的简单 API:

        release_year 与流派

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

        流派与发布年份

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-07
          • 2021-06-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多