【问题标题】:Creating subplots from a list of sub dataframes in Pandas从 Pandas 中的子数据框列表创建子图
【发布时间】:2020-11-12 04:33:37
【问题描述】:

我正在kaggle 上进行视频游戏销售项目。

该数据的前5条记录为(取出几列留出空间):

        Name                Platform    Genre        Publisher  NA_Sales    EU_Sales    JP_Sales    Global_Sales
0   Wii Sports                Wii      Sports        Nintendo   41.49       29.02        3.77        82.74
1   Super Mario Bros.         NES      Platform      Nintendo   29.08       3.58         6.81        40.24
2   Mario Kart Wii            Wii      Racing        Nintendo   15.85       12.88        3.79        35.82
3   Wii Sports Resort         Wii      Sports        Nintendo   15.75       11.01        3.28        33.00
4   Pokemon Red/Pokemon Blue  GB       Role-Playing  Nintendo   11.27       8.89         10.22       31.37

我希望通过Platform(控制台)比较索尼和任天堂的销量。所以到目前为止我所做的是首先创建索尼sub dataframes,例如通过sony_spt = sony.loc[sony['Genre']=='Sports'].drop(columns=['Year', 'Publisher']),然后plot。 此示例创建plot

# Sony sports sub df
sony_spt.groupby('Platform').sum().plot(kind='bar', figsize=(15, 7))

plt.xlabel('Platform')
plt.ylabel('Sales in $1m\'s')
plt.title('Sony sales in games per console for Sports genre')
plt.show()

我已将我为任天堂和索尼收集的其他子 dataframes 放在 list 中。例如,我的索尼流派列表是sony_genre_list = [sony_spt, sony_msc, sony_puz, sony_sim, sony_sgy, sony_ftg, sony_adv, sony_rpy, sony_sht, sony_rac, sony_plt, sony_act]。列出的所有类型包括:运动、杂项、解谜、模拟、策略、格斗、冒险、角色扮演、射击、赛车、平台游戏和动作。

我怎样才能节省时间并制作subplots(就像我演示的那样)listsub df's 来比较每个控制台/平台的类型销售?

【问题讨论】:

    标签: python pandas matplotlib subplot


    【解决方案1】:

    你可以试试zip:

    sony_genre_list = [sony_spt, sony_msc, sony_puz, sony_sim, sony_sgy, sony_ftg, sony_adv, sony_rpy, sony_sht, sony_rac, sony_plt, sony_act]
    
    # custumize nrows, ncols, figsize
    fig, axes = plt.subplots(nrows=len(sony_genre_list), ncols=1, 
                             figsize=(7,30))
    
    for df, ax in zip(sony_genre_list, axes.ravel()):
        df.groupby('Platform').sum().plot(kind='bar', ax=ax)
    

    你也可以用groupby绕过sony_genre_list的创建:

    for genre, df in sony.groupby('Genre'):
        ax = df.groupby('Platform').sum().plot(kind='bar')
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2020-04-13
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多