Pandas - Using pandas.pivot

The pandas.pivot(data, index=None, columns=None, values=None) function returns reshaped DataFrame organized by given index / column values. 
Click here for the documentation.
 
import pandas as pd
import numpy as np
 
df_to_pivot = pd.read_csv('to_pivot.csv', header=0)
df_to_pivot.head()
 
df_to_pivot['HEAD'].unique()
array(['BASIC', 'DA', 'HRA', 'SPECIAL'], dtype=object)
df_to_pivot = pd.pivot(df_to_pivot, index=['EMP_NO'], columns=['HEAD'], 
                       values=['AMOUNT'])
df_to_pivot.head()
 
df_to_pivot.columns = [str(j) + '_' + str(i) for i, j in df_to_pivot.columns]
df_to_pivot.head()
 
df_to_pivot.reset_index(inplace=True) 
df_to_pivot

Post a Comment

0 Comments