合并pandas数据透视表

时间:2017-05-22 19:42:53

标签: python pandas pivot-table

我有一个这样的数据框:

var isUserAuthed = false;
var user = null;
firebase.auth().onAuthStateChanged(function(newUser) {
  isUserAuthed = true;
  user = newUser;
})

function checkPublishButton() {
  $("#publish_button").click(function() {
    //Check if a user is logged in.
    if(isUserAuthed){
      //Declare firebase data-base
      var database = firebase.database();
      //Get user's total event created counter
      return database.ref("users/" + user.uid).once('value').then(function(snapshot) {
        console.log("This is not executing");
        return snapshot.val().total_event_created;
      }).then(function(counter) {
        //Increase the total_event_counter by one
        console.log("This is also not executing");
        var counter = counter + 1;
        database.ref('users/' + user_id + '/total_event_created').set(counter);
        //Get input from form
        var title = $("#title").val();
        var date = $("#date").val();
        var hour = $("#hour").val();
        var place = $("#place").val();
        var brief_description = $("#brief_description").val();
        var detailed_description = $("#detailed_description").val();
        var contact_email = $("#contact_email").val();
        var contact_phone_number = $("#contact_phone_number").val();
        var imageUrl = $("#imageUrl").val();
        //Post event into firebase database
        database.ref('events/' + user_id + '/' + counter).set({
          title: title,
          date: date,
          hour: hour,
          place: place,
          brief_description: brief_description,
          detailed_description: detailed_description,
          contact_email: contact_email,
          contact_phone_number: contact_phone_number,
          imageUrl: imageUrl
        });
      });
    }

}
}

我正在寻找的结果将成为一个支点,包括功能的结果计数以及执行时间的总和。喜欢这个

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 
......

我获得了单独的数据帧以获取结果计数的枢轴和按功能执行时间的总和,但我无法合并这些数据帧以获得我的最终预期结果。

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*

2 个答案:

答案 0 :(得分:1)

您无需在此处合并结果,您可以使用单个pivot_table或groupby / apply创建此结果。我没有你的数据,但是这可以得到你想要的吗?

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

答案 1 :(得分:0)

#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