Search code examples
pythonpandasdataframeeval

Splitting a Dataframe of a Dict. to a new DataFrame with keys and values


I have a dataframe of strings. The current dataframe looks like this: Current dataframe

Each datapoint contains a Dictionary like below:

"{'Index': 1, 'TimeSpent': 74088, 'RealInc': 'Obstacle_bef', 'IdentifiedIncident': 'Obstacle', 'TrLev': 7, 'TakeOverDecision': 'stay_put'},{'Index': 2, 'TimeSpent': 11336, 'RealInc': 'Obstacle_after_success', 'IdentifiedIncident': 'Pedestrian', 'TrLev': 7 },{'Index': 3, 'TimeSpent': 38594, 'RealInc': 'Cyclist_before', 'IdentifiedIncident': 'Cyclist', 'TrLev': 7, 'TakeOverDecision': 'stay_put'},{'Index': 4, 'TimeSpent': 16011, 'RealInc': 'Cyclist_after_success', 'IdentifiedIncident': 'Pedestrian', 'TrLev': 7 }".

I would like to make a new dataframe where each colomn represents the key of that dictionary. I have tried to use eval(), as well as using apply like this. But I think because every other dict is missing the key of 'TakeOverDecision', the apply does not work on it.

Any suggestions or guidance on how to split this to a dataset which make my dataset looks like below would be great! Desired dataframe


Solution

  • Maybe this could help.

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html

    import pandas as pd
    data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
    pd.DataFrame.from_dict(data)
       col_1 col_2
    0      3     a
    1      2     b
    2      1     c
    3      0     d