Convert a column containing a list of dictionaries to multiple columns in pandas dataframe

后端 未结 1 1146
醉话见心
醉话见心 2021-01-19 00:16

I have a Pandas dataframe like :

pd.DataFrame({\'a\':[1,2], \'b\':[[{\'c\':1,\'d\':5},{\'c\':3, \'d\':7}],[{\'c\':10,\'d\':50}]]})
Out[2]: 
   a                     


        
1条回答
  •  伪装坚强ぢ
    2021-01-19 00:42

    You can use concat with list comprehension:

    df = pd.concat([pd.DataFrame(x) for x in df['b']], keys=df['a'])
           .reset_index(level=1, drop=True).reset_index()
    
    print (df)
       a   c   d
    0  1   1   5
    1  1   3   7
    2  2  10  50
    

    EDIT:

    If index is unique, then is possible use join for all columns:

    df1 = pd.concat([pd.DataFrame(x) for x in df['b']], keys=df.index)
            .reset_index(level=1,drop=True)
    df = df.drop('b', axis=1).join(df1).reset_index(drop=True)
    print (df)
       a   c   d
    0  1   1   5
    1  1   3   7
    2  2  10  50
    

    I try simplify solution:

    l = df['b'].str.len()
    df1 = pd.DataFrame(np.concatenate(df['b']).tolist(), index=np.repeat(df.index, l))
    df = df.drop('b', axis=1).join(df1).reset_index(drop=True)
    print (df)
       a   c   d
    0  1   1   5
    1  1   3   7
    2  2  10  50
    

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