Splitting dictionary/list inside a Pandas Column into Separate Columns

后端 未结 12 1322
南方客
南方客 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:40

    Merlin's answer is better and super easy, but we don't need a lambda function. The evaluation of dictionary can be safely ignored by either of the following two ways as illustrated below:

    Way 1: Two steps

    # step 1: convert the `Pollutants` column to Pandas dataframe series
    df_pol_ps = data_df['Pollutants'].apply(pd.Series)
    
    df_pol_ps:
        a   b   c
    0   46  3   12
    1   36  5   8
    2   NaN 2   7
    3   NaN NaN 11
    4   82  NaN 15
    
    # step 2: concat columns `a, b, c` and drop/remove the `Pollutants` 
    df_final = pd.concat([df, df_pol_ps], axis = 1).drop('Pollutants', axis = 1)
    
    df_final:
        StationID   a   b   c
    0   8809    46  3   12
    1   8810    36  5   8
    2   8811    NaN 2   7
    3   8812    NaN NaN 11
    4   8813    82  NaN 15
    

    Way 2: The above two steps can be combined in one go:

    df_final = pd.concat([df, df['Pollutants'].apply(pd.Series)], axis = 1).drop('Pollutants', axis = 1)
    
    df_final:
        StationID   a   b   c
    0   8809    46  3   12
    1   8810    36  5   8
    2   8811    NaN 2   7
    3   8812    NaN NaN 11
    4   8813    82  NaN 15
    

提交回复
热议问题