Python / Pandas - 基于多维数据

Python / Pandas - Pivot on multidimensional data

我是 python/pandas 的新手。我正在尝试将行转置为 columns.Apologies 以防出现任何误解 input_df(pic) output_df(pic)

input_df:
Date    Project    Processes                Time_in_sec Time_measures
-----------------------------------------------------------------------
7/6/2017    FE      eBanking .aspx          157         Average Response Time 
9/2/2017    PCB     eBanking Frontpage.fi   227         Call per hour
1/23/2017   ICC     Acct Transfer.dc        28          Average Response Time 
1/24/2017   PCB     Transaction .com        0           Number of calls
1/23/2017   ICC     eBanking Logon.no       0           Number of calls

output_df:
Date    Project Processes   Average Response Time   Call per hour   Number of calls
-----------------------------------------------------------------------------
7/6/2017    FE  eBanking .aspx        157       
9/2/2017    PCB eBanking Frontpage.fi                    227    
1/24/2017   PCB Transaction .com                                        0
1/23/2017   ICC Acct Transfer.dc      28        
1/23/2017   ICC eBanking Logon.no                                       0

我尝试了下面的代码,在输出 below_code's_output 中只得到了 3 列 'Average Response Time'、'Call per hour' 'Number of calls'。但我很困惑如何获得所有必要的 6 列。你能帮忙吗?

    output_df = input_df.pivot(columns = 'Time_measures',  values= 'Time_in_sec')

IIUC 您可以将 pivot_tablereset_index

一起使用
df.pivot_table(columns='Time_measures', values='Time_in_sec',index=['Date','Project','Processes'],fill_value='').reset_index()
Out[98]: 
Time_measures       Date Project              Processes  \
0              1/23/2017     ICC       Acct Transfer.dc   
1              1/23/2017     ICC      eBanking Logon.no   
2              1/24/2017     PCB       Transaction .com   
3               7/6/2017      FE         eBanking .aspx   
4               9/2/2017     PCB  eBanking Frontpage.fi   
Time_measures Average Response Time  Call per hour Number of calls  
0                                 28                                
1                                                                0  
2                                                                0  
3                                157                                
4                                              227