Search code examples
pythonpandaspandas-groupbydata-munging

Is there a Python library to group values of Col B based on Col A and display all values of a group in a single row?


I want the following data to be converted like the below expected output. Values of 2nd column must be grouped and displayed in a single row based on values in 1st column. I can use my own logic in Python to do this, but wanted to know if Pandas or any other library in Python provides such functionality to make it easier.

Input:

1   A
1   B
1   B
2   D
3   A
1   C
4   D
2   A
1   A
3   B
4   A
2   D
2   C
4   C
4   B
4   A
3   A

The expected result:

1   A   B   B   C   A
2   D   A   D   C   
3   A   B   A       
4   D   A   C   B   A

Solution

  • Use assuming 0 is the first column and 1 is the second. if first column is index replace 0 with df.index

    m=df.groupby(0)[1].apply(list)
    print(pd.DataFrame(m.values.tolist(),index=m.index).rename_axis(None).fillna(''))
    

       0  1  2  3  4
    1  A  B  B  C  A
    2  D  A  D  C   
    3  A  B  A      
    4  D  A  C  B  A