我正在从包含
的表中获取数据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')
答案 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