【问题标题】:ploting pie chart using groupby function in pandas and plotly在 pandas 和 plotly 中使用 groupby 函数绘制饼图
【发布时间】:2020-10-12 17:21:55
【问题描述】:

我正在尝试根据 dataframe 上的函数 groupby 绘制饼图。

我能够返回 groupby 函数的正确结果,但是当尝试绘制饼图时,它不会显示所有结果。

代码:

tl = pd.unique(df['event_type'].tolist())

th = pd.unique(df['event_mohafaza'].tolist())

ct = df.groupby(['event_mohafaza','event_type']).aggregate('sum')

ct = ct.reset_index()
ct['labels'] = df['event_mohafaza'] + ' ' + df['event_type']

trace = go.Pie(labels=ct['labels'], 
                hoverinfo='label+percent', 
                values=ct['number_person'], 
                textposition='outside',                
                rotation=90)
layout = go.Layout(
                   title="Percentage of events",
                   font=dict(family='Arial', size=12, color='#909090'),
                   legend=dict(x=0.9, y=0.5)
                    )
data = [trace]
fig = go.Figure(data=data, layout=layout)
fig.show()

groupby 函数的结果:

                                 number_person
event_mohafaza event_type                     
loc1           swimming                9157
               football                690
               baseball                2292

loc2           swimming               10560
               football               8987
               baseball               70280
              
loc3           basketball             130
               swimming               19395
               football               5370
               baseball               19078
              
loc4           swimming               9492
               football               50
               baseball               5279

loc5           swimming               4652
               football               2215
               baseball               3000

绘制的饼图:

它没有显示它必须将馅饼分成 16 块的所有值,现在它被分成 8 块

【问题讨论】:

  • 能否提供您的数据,或者一些示例数据?

标签: python pandas pandas-groupby pie-chart


【解决方案1】:

尝试使用:

values = ct['number_person'].value_counts()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2017-08-10
    • 2014-02-01
    相关资源
    最近更新 更多