0

I have a Pandas dataframe in the following format:

ID|Date|Values
1234|2021-01-01|{"Reason":"Change", "New Value":"Segment 2", "Old Value":"Segment 1"}

I'd like to parse the values column and create a new dataframe:

ID|Date|Old|New

The order of the values is sometimes different. How can I extract these values in Python?

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Does this answer your question? [Split / Explode a column of dictionaries into separate columns with pandas](https://stackoverflow.com/questions/38231591/split-explode-a-column-of-dictionaries-into-separate-columns-with-pandas) – David Kaftan Jun 02 '21 at 21:36

2 Answers2

0
df[["Old", "New"]] = df["Values"].apply(lambda d: (d["Old Value"], d["New Value"])) \
                                 .tolist()
df = df.drop(columns="Values")

Another method:

import operator

old = operator.itemgetter("Old Value")
new = operator.itemgetter("New Value")

df["Old"] = df["Values"].apply(old)
df["New"] = df["Values"].apply(new)
df = df.drop(columns="Values")
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

You can use pd.DataFrame() to extract the dictionary into columns. Take the 2 related columns from the resulting new dataframe and join it with the original dataframe using .join() and drop the original Values column by .drop().

df_new = df.drop('Values', axis=1).join(pd.DataFrame(df['Values'].tolist())[['Old Value', 'New Value']])

Note that making use of pd.DataFrame() for extracting dictionary into columns is the fastest among various ways of doing the same task. It is considerably faster than using .apply() with lambda function.

Ressult:

print(df_new)


     ID        Date  Old Value  New Value
0  1234  2021-01-01  Segment 1  Segment 2
SeaBean
  • 22,547
  • 3
  • 13
  • 25