Skip to content
Advertisement

Convert a list of nested dictionary WITH STRING OBJECT into pandas Dataframe

I have a real-time url “linktoAPI” containing a list of nested dictionary. I have tried many solutions from the links below, but none of them helps me achieve what I want. Apparently, it is because the value of the nested dictionary (key: “history_value”) is ununiform and contain long string object.

Extract dictionary value from column in data frame (got the error: ‘get’ does not applicable for string)

How to convert list of nested dictionary to pandas DataFrame? (got the error:’str’ object is not a mapping)

Split / Explode a column of dictionaries into separate columns with pandas,

https://www.skytowner.com/explore/splitting_dictionary_into_separate_columns_in_pandas_dataframe

JavaScript

Run this code and you can see that the column “history_value” contain an ununiform dictionary. I want this columns to be separated into “ngaybaoluu”, “ngayhoclai” and “lydo”. Please help me. Thank you very much!!!

Advertisement

Answer

If I get it right, this should work:

JavaScript

this df should be like

JavaScript

then we select the needed columns ngaybaoluu, ngayhoclai and lydo with

JavaScript

the final df is

JavaScript

Be aware that many of the columns have null values in them, which means the original response of the url does not contain these fields, so it’s fine. If you want to fill these null values, you can look up to .fillna().

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement