0

I have a json object which is returned by an API. I iterate over json data and obtain a list of dictionaries. I'd like to iterate over this list of dictionaries and generate a pandas dataframe.

json_data = 

{'status': 'REQUEST_SUCCEEDED',
 'responseTime': 136,
 'message': [],
 'Results': {'series': [{'seriesID': 'SMS04380600000000001',
 'data': [{'year': '2020',
           'period': 'M10',
           'periodName': 'October',
           'latest': 'true',
           'value': '2148.7',
           'footnotes': [{'code': 'P', 'text': 'Preliminary'}]},

          {'year': '2020',
           'period': 'M09',
           'periodName': 'September',
           'value': '2131.8',
           'footnotes': [{}]},

          {'year': '2020',
           'period': 'M08',
           'periodName': 'August',
           'value': '2119.9',
           'footnotes': [{}]},

          {'year': '2020',
           'period': 'M07',
           'periodName': 'July',
           'value': '2098.1',
           'footnotes': [{}]},

]}


# Pandas DataFrame
df = pd.DataFrame()

# Iterate over data for each series

for series in json_data['Results']['series']:
    
    for data in series['data']:
        
        print(data)

        # Populate Pandas DataFrame code goes here

        df['Year'] == 

       


{'year': '2020', 'period': 'M10', 'periodName': 'October', 'latest': 'true', 'value': '2148.7', 
'footnotes': [{'code': 'P', 'text': 'Preliminary'}]}
{'year': '2020', 'period': 'M09', 'periodName': 'September', 'value': '2131.8', 'footnotes': [{}]}
{'year': '2020', 'period': 'M08', 'periodName': 'August', 'value': '2119.9', 'footnotes': [{}]}
.
.
.

Additionally, I need to delete the footnotes key from each dictionary.

kms
  • 1,810
  • 1
  • 41
  • 92

1 Answers1

0

If data is your list of dictionaries, then you just need to do:

df = pd.DataFrame(data)

as suggested in this answer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
robinood
  • 1,138
  • 8
  • 16