简体   繁体   English

合并熊猫数据透视表

[英]merge pandas pivot tables

I have a dataframe like this: 我有一个这样的数据框:

Application|Category|Feature|Scenario|Result|Exec_Time  
A1|C1|F1|scenario1|PASS|2.3 
A1|C1|F1|scenario2|FAIL|20.3
A2|C1|F3|scenario3|PASS|12.3 
......

The outcome i am looking for will be a pivot with count of results by Feature and also the sum of exec times. 我要寻找的结果将是一个枢轴,其中包含Feature的结果计数以及exec时间的总和。 Like this 像这样

Application|Category|Feature|Count of PASS|Count of FAIL|SumExec_Time     
A1|C1|F1|200|12|45.62
A1|C1|F2|90|0|15.11
A1|C2|F3|97|2|33.11*

I got individual dataframes to get the pivots of result counts and the sum of execution time by feature but I am not able to merge those dataframes to get my final expected outcome. 我得到了单独的数据框,以按功能获取结果计数和执行时间总和的数据,但我无法合并这些数据框以得到最终的预期结果。

dfr = pd.pivot_table(df,index=["Application","Category","Feature"],
values=["Final_Result"],aggfunc=[len])

dft = pd.pivot_table(df,index=["Application","Category","Feature"],
values=["Exec_time_mins"],aggfunc=[np.sum])

You don't need to merge results here, you can create this with a single pivot_table or groupby/apply. 您无需在此处合并结果,可以使用单个pivot_table或groupby / apply创建结果。 I don't have your data but does this get you what you want? 我没有您的数据,但是这能为您带来想要的东西吗?

pivot = pd.pivot_table(df, index=["Application","Category","Feature"], 
                           values = ["Final_Result", "Exec_time_mins"], 
                           aggfunc = [len, np.sum])
#Count total records, number of FAILs and total time.
df2 = df.groupby(by=['Application','Category','Feature']).agg({'Result':[len,lambda x: len(x[x=='FAIL'])],'Exec_Time':sum})

#rename columns
df2.columns=['Count of PASS','Count of FAIL','SumExec_Time']

#calculate number of pass
df2['Count of PASS']-=df2['Count of FAIL']

#reset index
df2.reset_index(inplace=True)

df2
Out[1197]: 
  Application Category Feature  Count of PASS  Count of FAIL  SumExec_Time
0          A1       C1      F1              1              1          22.6
1          A2       C1      F3              1              0          12.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM