Groupby, transpose and append in Pandas?

前端 未结 1 1871
小鲜肉
小鲜肉 2021-02-13 05:58

I have a dataframe which looks like this:

Each user has 10 records. Now, I want to create a dataframe which looks like this:

<
相关标签:
1条回答
  • 2021-02-13 06:08

    groupby('userid') then reset_index within each group to enumerate consistently across groups. Then unstack to get columns.

    df.groupby('userid')['name'].apply(lambda df: df.reset_index(drop=True)).unstack()
    

    Demonstration

    df = pd.DataFrame([
            [123, 'abc'],
            [123, 'abc'],
            [456, 'def'],
            [123, 'abc'],
            [123, 'abc'],
            [456, 'def'],
            [456, 'def'],
            [456, 'def'],
        ], columns=['userid', 'name'])
    
    df.sort_values('userid').groupby('userid')['name'].apply(lambda df: df.reset_index(drop=True)).unstack()
    

    if you don't want the userid as the index, add reset_index to the end.

    df.sort_values('userid').groupby('userid')['name'].apply(lambda df: df.reset_index(drop=True)).unstack().reset_index()
    

    0 讨论(0)
提交回复
热议问题