Thursday, September 4, 2025
HomeLanguagespandas.lreshape() function in Python

pandas.lreshape() function in Python

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




# importing package
import numpy
import pandas as pd
 
# create and view data
data = pd.DataFrame({
    'hr1': [514, 573],
    'hr2': [545, 526],
    'team': ['Red Sox', 'Yankees'],
    'year1': [2007, 2007],
    'year2': [2008, 2008]
})
print(data)
 
# use pandas.lreshape() method
print(pd.lreshape(data, {'year': ['year1', 'year2'],
                         'hr': ['hr1', 'hr2']}))
# This code is contributed by rakeshsahni


Output:

pandas lreshape()

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS