透视表后,Date列在表元数据中不可见- Python

gmxoilav  于 2023-01-29  发布在  Python
关注(0)|答案(2)|浏览(87)

我用python透视了我的表。我已经验证了所有列都是可见的。但是当我们查看信息时,日期列没有出现。当我们创建图表时,需要将日期作为X值。python说这是key error :Date
下面是查询

df2=pd.pivot_table(df,index='Date',values = 'Amount', columns = 'Type',aggfunc='sum')

输出:

Type        Customer Credit Note    Payment  Sales Invoice    Balance  \
Date                                                                    
2022-01-31                927.85  685435.45     1108054.27  421690.97   
2022-02-28                  0.00  666665.71     1158489.98  491824.27   
2022-03-31              31174.00  726719.20      908525.44  150632.24   
2022-04-30                  0.00       0.00      967592.69  967592.69   

Type        cumsum_reverse  OS for the month  limit vs purchases ratio  \
Date                                                                     
2022-01-31      1610049.20        2474027.18                  0.271311   
2022-02-28      1118224.93        2965851.45                  0.283660   
2022-03-31       967592.69        3116483.69                  0.222456   
2022-04-30            0.00        4084076.38                  0.236918   

Type        OS vs Payment ratio  OS vs limit ratio  
Date                                                
2022-01-31             0.277053           0.618507  
2022-02-28             0.224781           0.741463  
2022-03-31             0.233186           0.779121  
2022-04-30             0.000000           1.021019

当我们尝试df2.info()输出时:

class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 4 entries, 2022-01-31 to 2022-04-30
Data columns (total 9 columns):
      Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Customer Credit Note      4 non-null      float64
 1   Payment                   4 non-null      float64
 2   Sales Invoice             4 non-null      float64
 3   Balance                   4 non-null      float64
 4   cumsum_reverse            4 non-null      float64
 5   OS for the month          4 non-null      float64
 6   limit vs purchases ratio  4 non-null      float64
 7   OS vs Payment ratio       4 non-null      float64
 8   OS vs limit ratio         4 non-null      float64
dtypes: float64(9)
memory usage: 320.0 bytes

正如你所看到的,信息表中缺少日期列,它被指定为日期时间索引。另外,我确实需要基于这些列创建一个预测图表。
(Data,OS与限制比率),但当我运行查询时,它显示key error :Date
有人能帮我解决这个问题吗?

jchrr9hc

jchrr9hc1#

可以指定对象而不是字符串作为pivot_table的索引或列参数:

df2 = pd.pivot_table(df, index=df.index, values='Amount', columns='Type', aggfunc='sum')
#                         HERE ---^
exdqitrt

exdqitrt2#

在进行透视时,您将Date列设置为索引。如果需要Date列,也许可以通过执行以下操作重置索引

df = df.reset_index()

这将从索引中删除Date列,并将其设置为单独的列。

相关问题