Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading