After pivoting the table, Date column is not visible in table matadata – Python

Question:

I have pivot my table using python. And I have verified that all the columns are visible. But when we view the info , the date column is not appearing. And when we are creating a graph, it is required to put the date as X value. Python says that is a key error :Date

Below is the query

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

Output :

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  

When we try out df2.info()
Output :

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

As you can see, the date column is missing from the info table and it is stating as date time index. Also, I do need to create a foreasting chart based on these columns.

(Data,OS vs limit ratio). But when I run the query, it says key error :Date

Can anyone help me to sort out this issue?

Asked By: Brian Michaels

||

Answers:

You set the Date column as an index when you did the pivoting. If you need the Date column, perhaps you can reset the index by doing

df = df.reset_index()

This will remove the Date column from being an index and set it as a separate column.

Answered By: swphsil

You can specify an object instead of a string as index or columns parameters for pivot_table:

df2 = pd.pivot_table(df, index=df.index, values='Amount', columns='Type', aggfunc='sum')
#                         HERE ---^
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.