熊猫Pivot_Table日期时间错误排序

时间:2020-06-17 15:05:44

标签: python pandas datetime

这是dataframe

的部分输出
                      Date     Value     Entity
0     01/01/2016, 10:30:00  110.778447      Aag
1     02/01/2016, 10:30:00  110.680482      Aag
2     03/01/2016, 10:30:00  110.574833      Aag
3     04/01/2016, 10:30:00  110.461546      Aag
4     05/01/2016, 10:30:00  110.340676      Aag
5     06/01/2016, 10:30:00  110.212280      Aag
6     07/01/2016, 10:30:00  110.076422      Aag

当我将pd.pivot_table应用于此dataframe

results = pd.pivot_table(dataframe1, values='Value', index=['Date'], columns=['Entity'], aggfunc=np.sum,fill_value=0)

我得到一个奇怪的date,如下所示

Date                                             ...                            
01/01/2016, 10:30:00  
01/02/2016, 10:30:00  
01/03/2016, 10:30:00  
01/04/2016, 10:30:00  
01/05/2016, 10:30:00  
01/06/2016, 10:30:00  
01/07/2016, 10:30:00  
01/08/2016, 10:30:00  
01/09/2016, 10:30:00  
01/10/2016, 10:30:00  
01/11/2016, 10:30:00  
01/12/2016, 10:30:00  
02/01/2016, 10:30:00  
02/02/2016, 10:30:00  
02/03/2016, 10:30:00  

我在这里缺少什么,对此将提供任何帮助。

1 个答案:

答案 0 :(得分:1)

您获得的输出看起来像是按字符串字符排序的-在数据透视之前将列解析为具有适当格式的datetime dtype,例如

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y, %H:%M:%S')