parsing a dictionary in a pandas dataframe cell into new row cells (new columns)

前端 未结 2 1734
孤城傲影
孤城傲影 2020-12-17 20:47

I have a Pandas Dataframe that contains one column containing cells containing a dictionary of key:value pairs, like this:

{\"name\":\"Test Thorton\",\"compa         


        
2条回答
  •  失恋的感觉
    2020-12-17 21:24

    consider df

    df = pd.DataFrame([
            ['a', 'b', 'c', 'd', dict(F='y', G='v')],
            ['a', 'b', 'c', 'd', dict(F='y', G='v')],
        ], columns=list('ABCDE'))
    
    df
    
       A  B  C  D                     E
    0  a  b  c  d  {'F': 'y', 'G': 'v'}
    1  a  b  c  d  {'F': 'y', 'G': 'v'}
    

    Option 1
    Use pd.Series.apply, assign new columns in place

    df.E.apply(pd.Series)
    
       F  G
    0  y  v
    1  y  v
    

    Assign it like this

    df[['F', 'G']] = df.E.apply(pd.Series)
    df.drop('E', axis=1)
    
       A  B  C  D  F  G
    0  a  b  c  d  y  v
    1  a  b  c  d  y  v
    

    Option 2
    Pipeline the whole thing using the pd.DataFrame.assign method

    df.drop('E', 1).assign(**pd.DataFrame(df.E.values.tolist()))
    
       A  B  C  D  F  G
    0  a  b  c  d  y  v
    1  a  b  c  d  y  v
    

提交回复
热议问题