pandas.lreshape() function in Python
Last Updated :
24 Aug, 2021
This method is used to reshape long-format data to wide. This is the generalized inverse of DataFrame.pivot.
Syntax : pandas.lreshape(data, groups, dropna=True, label=None)
Arguments :
- data : DataFrame
- groups : dict {new_name : list_of_columns}
- dropna : boolean, default True
Below is the implementation of the above method with an example :
Python3
import numpy
import pandas as pd
data = pd.DataFrame({
'hr1' : [ 514 , 573 ],
'hr2' : [ 545 , 526 ],
'team' : [ 'Red Sox' , 'Yankees' ],
'year1' : [ 2007 , 2007 ],
'year2' : [ 2008 , 2008 ]
})
print (data)
print (pd.lreshape(data, { 'year' : [ 'year1' , 'year2' ],
'hr' : [ 'hr1' , 'hr2' ]}))
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...