Search code examples
pythonpandaspivot-tablepandas-groupbydata-munging

python pandas create a list group by value


I have a dataframe in python:

    pID     sID     time 
0   2133    152414  2018-06-16
1   1721    152912  2018-06-17
2   2264    152912  2018-06-18

I want to create a new table with sID as the key and list of pID:

        pID time
152414 2133 2018-06-16
152912 1721 2018-06-17
       2264 2018-06-18

What is the best way to do it without iterating over all the dataframe? I tried:

df.pivot(index='sID', columns=['pID', 'time'])

But got:

ValueError: all arrays must be same length

For these table of 3 rows Thanks!


Solution

  • Try this:

    import io
    import pandas as pd
    
    f = io.StringIO('''
    2133    152414  2018-06-16
    1721    152912  2018-06-17
    2264    152912  2018-06-18''')
    
    df = pd.read_csv(f, sep='\s+', header=None, names=['pID', 'sID', 'date'])
    df.set_index(['sID', 'pID'])
    

    Results