【问题标题】:How can normalize and plot this data?如何标准化和绘制这些数据?
【发布时间】:2020-03-03 09:16:03
【问题描述】:

我正在尝试在堆叠条中绘制以下数据,但我不知道如何以最佳方式进行。

我有以下数据:

**player**     **action**         **count**
player1        lost               15
               win                3
player2        lost               78
               win                61
               leave              6
                                  ..
player100      lost               43
               win                22
               leave              9
player101      lost               5
               leave              4

这是一个熊猫系列,所以在每个“玩家”属性中都可以与三个不同的动作[输、赢、离开]相关联,有些玩家只有另外两个和另外三个中的一个。

我想对这些数据进行规范化,并用这样的桩柱绘制。

我从

获取数据
df_players = df.groupby('players')['action'].value_counts()

所以我真的无法像一行一样访问计数

【问题讨论】:

    标签: python pandas matplotlib pandas-groupby


    【解决方案1】:

    您可以将计数除以总和:

    (df['count'].div(df.['count'].sum(level='player'))
                .mul(100)
                .unstack('action')
                .plot.barh(stacked=True)
    )
    

    更新您的评论,您可以:

    (df.groupby('players')['action']
       .value_counts(normalize=True)
       .unstack('action')
       .plot.barh(stacked=True)
    )  
    

    【讨论】:

    • 我无法从这里获取 count 属性 df_players = df.groupby('players')['action'].value_counts()
    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2018-04-12
    • 2015-01-03
    • 1970-01-01
    • 2022-09-25
    • 2018-05-14
    相关资源
    最近更新 更多