Home > Mobile >  Creating new dataframe columns based on dict from each cell
Creating new dataframe columns based on dict from each cell

Time:10-13

I have a dataframe that looks like this:

                                                   0                                                  1
0  {'time': '1662249600', 'formattedTime': 'Sep 4...  {'time': '1663459200', 'formattedTime': 'Sep 1...
1  {'time': '1662336000', 'formattedTime': 'Sep 5...  {'time': '1663545600', 'formattedTime': 'Sep 1...
2  {'time': '1662422400', 'formattedTime': 'Sep 6...  {'time': '1663632000', 'formattedTime': 'Sep 2...
3  {'time': '1662508800', 'formattedTime': 'Sep 7...  {'time': '1663718400', 'formattedTime': 'Sep 2...
4  {'time': '1662595200', 'formattedTime': 'Sep 8...  {'time': '1663804800', 'formattedTime': 'Sep 2...

where each cell looks like this: {'time': '1662249600', 'formattedTime': 'Sep 4, 2022', 'value': 47, 'formattedValue': '47', 'isPartial': False}

How can I pull a specific value (e.g. time) from each dict and add it to new columns. The result would be something like this:

   time_0       time_1
0  1662249600   1663459200
1  1662336000   1663545600
2  1662422400   1663632000
3  1662508800   1663718400
4  1662595200   1663804800

CodePudding user response:

If your data inside columns are dictionaries, you can do:

df["time_0"] = df[0].apply(pd.Series)["time"]
df["time_1"] = df[1].apply(pd.Series)["time"]

df = df.drop([0,1], axis=1)
  • Related