Splitting dictionary/list inside a Pandas Column into Separate Columns

后端 未结 12 1457
南方客
南方客 2020-11-22 02:50

I have data saved in a postgreSQL database. I am querying this data using Python2.7 and turning it into a Pandas DataFrame. However, the last column of this dat

12条回答
  •  执念已碎
    2020-11-22 03:38

    To convert the string to an actual dict, you can do df['Pollutant Levels'].map(eval). Afterwards, the solution below can be used to convert the dict to different columns.


    Using a small example, you can use .apply(pd.Series):

    In [2]: df = pd.DataFrame({'a':[1,2,3], 'b':[{'c':1}, {'d':3}, {'c':5, 'd':6}]})
    
    In [3]: df
    Out[3]:
       a                   b
    0  1           {u'c': 1}
    1  2           {u'd': 3}
    2  3  {u'c': 5, u'd': 6}
    
    In [4]: df['b'].apply(pd.Series)
    Out[4]:
         c    d
    0  1.0  NaN
    1  NaN  3.0
    2  5.0  6.0
    

    To combine it with the rest of the dataframe, you can concat the other columns with the above result:

    In [7]: pd.concat([df.drop(['b'], axis=1), df['b'].apply(pd.Series)], axis=1)
    Out[7]:
       a    c    d
    0  1  1.0  NaN
    1  2  NaN  3.0
    2  3  5.0  6.0
    

    Using your code, this also works if I leave out the iloc part:

    In [15]: pd.concat([df.drop('b', axis=1), pd.DataFrame(df['b'].tolist())], axis=1)
    Out[15]:
       a    c    d
    0  1  1.0  NaN
    1  2  NaN  3.0
    2  3  5.0  6.0
    

提交回复
热议问题