Explode a DataFrame column and add the objects to the dataframe


i am fairly new to Pandas and DataFrame Analysis and i’m trying to handle a DataFrame which i got from a Web API formerly as json. I managed to turn it into a DataFrame and arrange the Data that i need.

But there still is one column that i havent been able to split up.

#The column ['greeks'] contains data as follows:

   "greeks": {
      "delta": "string",
      "gamma": "string",
      "rho": "string",
      "theta": "string",
      "vega": "string"

I tried using df.explode(‘greeks’) but that just would remove each string value.

What is the best approach to split and add "delta","gamma","rho","theta","vega" to the dataframe as a column each? I’m sorry if this is a trivial action but i did my research and couldn’t find a solution.

>Solution :

You can normalize the greeks column and then drop it:

temp_df = pd.json_normalize(df['greeks'])
df = pd.concat([df.drop('greeks', axis=1), temp_df], axis=1)

Leave a Reply Cancel reply