Pivot a pandas DataFrame to be the correct format: `DataError: No numeric types to aggregate`

后端 未结 4 1777
青春惊慌失措
青春惊慌失措 2020-12-21 06:47

Here is a pandas DataFrame I would like to manipulate:

import pandas as pd

data = {\"grouping\": [\"item1\", \"item1\", \"item1\", \"item2\", \"item2\", \"         


        
4条回答
  •  囚心锁ツ
    2020-12-21 07:06

    Use set_index and unstack:

    df = df.set_index(['grouping','labels']).unstack().rename_axis(None)
    df.columns = df.columns.droplevel()
    print(df)
    

    Output:

    labels  A    B    C     D
    item1   5    1    8  None
    item2   3  731  189     9
    

提交回复
热议问题