Home > Back-end >  How to extract list of dictionaries from Pandas column
How to extract list of dictionaries from Pandas column

Time:01-05

I have the following dataframe that I extracted from an API, inside that df there is a column that I need to extract data from it, but the structure of that data inside that column is a list of dictionaries:

enter image description here

I could get the data that I care from that dictionary using this chunk of code:

for k,v in d.items():
    for i,j in v.items():
        if isinstance(j, list):
            for l in range(len(j)):
                for k in j[l]:
                    print(j[l])

I get a structure like this one, so I´d need to get each of that 'values' inside the list of dictionaries

enter image description here

and then organize them in a dataframe. like for example the first item on the list of dictionaries:

enter image description here

Once I get to the point of getting the above structure, how could I make a dataframe like the one in the image?

Raw data:

data = {'rows': [{'values': ['Tesla Inc (TSLA)', '$1056.78', '$1199.78', '13.53%'], 'children': []}, {'values': ['Taiwan Semiconductor Manufacturing Company Limited (TSM)', '$120.31', '$128.80', '7.06%'], 'children': []}]}

CodePudding user response:

You can use pandas. First cast your data to pd.DataFrame, then use apply(pd.Series) to expand lists inside 'values' column to separate columns and set_axis method to change column names:

import pandas as pd
data = {'rows': [{'values': ['Tesla Inc (TSLA)', '$1056.78', '$1199.78', '13.53%'], 'children': []}, {'values': ['Taiwan Semiconductor Manufacturing Company Limited (TSM)', '$120.31', '$128.80', '7.06%'], 'children': []}]}
out = pd.DataFrame(data['rows'])['values'].apply(pd.Series).set_axis(['name','price','price_n','pct'], axis=1)

Output:

                                                    name     price   price_n     pct
    0                                   Tesla Inc (TSLA)  $1056.78  $1199.78   13.53%  
    1  Taiwan Semiconductor Manufacturing Company Lim...   $120.31   $128.80    7.06%  
      
  •  Tags:  
  • Related