Search code examples
pythonpandasdataframepandas-melt

Using melt() in Pandas


Trying to melt the following data such that the result is 4 rows - one for "John-1" containing his before data, one for "John-2" containing his after data, one for "Kelly-1" containing her before data, and one for "Kelly-2" containing her after data. And the columns would be "Name", "Weight", and "Height". Can this be done solely with the melt function?

df = pd.DataFrame({'Name': ['John', 'Kelly'],
                 'Weight Before': [200, 175], 
                 'Weight After': [195, 165],
                 'Height Before': [6, 5],
                 'Height After': [7, 6]})

Solution

  • Use pandas.wide_to_long function as shown below:

    pd.wide_to_long(df, ['Weight', 'Height'], 'Name', 'grp', ' ', '\\w+').reset_index()
    
        Name     grp  Weight  Height
    0   John  Before     200       6
    1  Kelly  Before     175       5
    2   John   After     195       7
    3  Kelly   After     165       6
    

    or you could also use pivot_longer from pyjanitor as follows:

    import janitor
    df.pivot_longer('Name', names_to = ['.value', 'grp'], names_sep = ' ')
    
        Name     grp  Weight  Height
    0   John  Before     200       6
    1  Kelly  Before     175       5
    2   John   After     195       7
    3  Kelly   After     165       6