数据透视表转换在窗格中生成值错误

时间:2018-11-13 01:55:47

标签: python pandas cursor pivot

我正在从包含

的表中获取数据
execution_time, type, status, process, sub_process, value 
2018-11-12 16:09:48.179547, backlog, started, p1, s1, 100
2018-11-12 16:09:48.179547, backlog, created, p1, s1, 1005
2018-11-12 16:09:48.179547, backlog, started, p2, s1, 500
2018-11-12 16:09:48.179547, V1, created, p1, s1, 10
2018-11-12 16:09:48.179547, V2, created, p1, s1, 15
2018-11-12 16:09:48.179547, backlog, started, p3, s1, 0
2018-11-12 16:09:48.179547, backlog, started, p4, s1, 45
2018-11-12 16:09:48.179547, V1, started, p4, s1, 400 

我想透视这些数据,使其像这样:

状态,进程,子进程,积压,V1,V2

(待办事项,V1和V2应该包含相应的值,如果存在) 这是我的代码。我收到

错误
  

pivot_table()为参数“值”获得了多个值

sql = "select * from temp.data_table"
cursor.execute(sql)
results = cursor.fetchall()
df = pd.DataFrame(results)
df=df.pivot_table(index=['status','process','sub_process'],columns='type',values='value', aggfunc = 'sum')

1 个答案:

答案 0 :(得分:1)

使用(删除df中的pivot_table

df.pivot_table(index=['status','process','sub_process'],columns='type',values='value', aggfunc = 'sum')
Out[85]: 
type                             V1    V2   backlog
status   process sub_process                       
 created  p1      s1           10.0  15.0    1005.0
 started  p1      s1            NaN   NaN     100.0
          p2      s1            NaN   NaN     500.0
          p3      s1            NaN   NaN       0.0
          p4      s1          400.0   NaN      45.0

pd.pivot_table

pd.pivot_table(df,index=['status','process','sub_process'],columns='type',values=['value'], aggfunc = 'sum')
Out[86]: 
                              value               
type                             V1    V2  backlog
status   process sub_process                      
 created  p1      s1           10.0  15.0   1005.0
 started  p1      s1            NaN   NaN    100.0
          p2      s1            NaN   NaN    500.0
          p3      s1            NaN   NaN      0.0
          p4      s1          400.0   NaN     45.0