pandas 如何在python中绘制数据透视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43529054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:26:29  来源:igfitidea点击:

How to plot pivot chart in python?

pythonpandaspivot-tablepivottable.js

提问by Mithil Amin

Currenly, I am new in python scripting I am using panda, pivottablejs for creating a script. I have one csv file and I read that csv file using panda and I got the table like this. enter image description here

目前,我是 Python 脚本的新手,我正在使用 panda、pivottablejs 来创建脚本。我有一个 csv 文件,我使用 panda 读取了该 csv 文件,然后得到了这样的表格。 在此处输入图片说明

Now, I want to generate the pivotchart using pivottablejs so for that I have to pass dataframe object in the pivot_ui();

现在,我想使用 pivottablejs 生成数据透视图,因此我必须在 pivot_ui(); 中传递数据帧对象;

I want to Plot in Pivot Chart the total number of issue status created for every OriginationPhase.

我想在数据透视图中绘制为每个 OriginationPhase 创建的问题状态总数。

So I tried something like this.

所以我尝试了这样的事情。

LabelsReviewedByDate = issues_df.groupby(['Status','OriginationPhase'])

pivot_ui(LabelsReviewedByDate)

I know this is wrong but I am new in python scripting. So help me to find the solution.

我知道这是错误的,但我是 Python 脚本的新手。所以帮我找到解决方案。

Thank you

谢谢

回答by Andrew L

You can just pass the dataframe right to pivot_ui:

您可以将数据框直接传递给pivot_ui:

import pandas as pd
from pivottablejs import pivot_ui

a= [ [1,'Requirements','bug'],[2,'Design','bug'],[3,'Testing','bug'],[4,'Requirements','bug'],[5,'Requirements','Inquiry'] ]

df  = pd.DataFrame(a,columns =['Issue#','OriginationPhase','Category'])

pivot_ui(df)

enter image description here

在此处输入图片说明

回答by galaxyan

The pivot_table method comes to solve this problem. It works like pivot, but it aggregates the values from rows with duplicate entries for the specified columns

pivot_table 方法来解决这个问题。它的工作方式类似于数据透视表,但它聚合来自具有指定列重复条目的行的值

a= [ [1,'Requirements','bug'],[2,'Design','bug'],[3,'Testing','bug'],[4,'Requirements','bug'],[5,'Requirements','Inquiry'] ]

df  = pd.DataFrame(a,columns =['Issue#','OriginationPhase','Category'])
df.pivot_table( index = 'Category',columns = 'OriginationPhase',aggfunc = lambda x: len(x) )  )

                 Issue#                     
OriginationPhase Design Requirements Testing
Category                                    
Inquiry             NaN            1     NaN
bug                   1            2       1