'Creating a pandas data frame from a JSON object

Objective: I have fetched in insights data from my Instagram account using Instagram Graph API. Below I have a JSON object (audience_insights['data'].

[{'name': 'audience_city',
  'period': 'lifetime',
  'values': [{'value': {'London, England': 1,
     'Kharkiv, Kharkiv Oblast': 1,
     'Jamui, Bihar': 1,
     'Burdwan, West Bengal': 1,
     'Kolkata, West Bengal': 112,
     'Dhulian, West Bengal': 1,
     'Argonne, Wisconsin': 1,
     'College Park, Georgia': 1,
     'Pakaur, Jharkhand': 1,
     'Bristol, England': 1,
     'Delhi, Delhi': 1,
     'Gaya, Bihar': 1,
     'Howrah, West Bengal': 1,
     'Kanpur, Uttar Pradesh': 1,
     'Jaipur, Rajasthan': 2,
     'Panipat, Haryana': 1,
     'Saint Etienne, Rhône-Alpes': 1,
     'Panagarh, West Bengal': 1,
     'Bhagalpur, Bihar': 1,
     'Frankfurt, Hessen': 1,
     'Riyadh, Riyadh Region': 1,
     'Roorkee, Uttarakhand': 1,
     'Harinavi, West Bengal': 1,
     'Secunderabad, Telangana': 1,
     'Mumbai, Maharashtra': 3,
     'Patna, Bihar': 11,
     'Obando, Valle del Cauca': 1,
     'Jaunpur, Uttar Pradesh': 1,
     'Sitamau, Madhya Pradesh': 1},
    'end_time': '2022-03-24T07:00:00+0000'}],
  'title': 'Audience City',
  'description': "The cities of this profile's followers",
  'id': '17841406112341342/insights/audience_city/lifetime'},
 {'name': 'audience_country',
  'period': 'lifetime',
  'values': [{'value': {'DE': 1,
     'IN': 144,
     'GB': 2,
     'UA': 1,
     'FR': 1,
     'CO': 1,
     'SA': 1,
     'US': 2},
    'end_time': '2022-03-24T07:00:00+0000'}],
  'title': 'Audience Country',
  'description': "The countries of this profile's followers",
  'id': '17841406112341342/insights/audience_country/lifetime'},
 {'name': 'audience_gender_age',
  'period': 'lifetime',
  'values': [{'value': {'F.13-17': 1,
     'F.18-24': 20,
     'F.25-34': 5,
     'M.13-17': 4,
     'M.18-24': 79,
     'M.25-34': 15,
     'M.35-44': 1,
     'M.45-54': 3,
     'U.13-17': 4,
     'U.18-24': 16,
     'U.25-34': 2,
     'U.45-54': 3},
    'end_time': '2022-03-24T07:00:00+0000'}],
  'title': 'Gender and Age',
  'description': "The gender and age distribution of this profile's followers",
  'id': '17841406112341342/insights/audience_gender_age/lifetime'}]

I wish to loop through this and create three data frames:

  1. First that shows demographic and count.
|      |    Location     | Count |
| ---- | --------------  | ----- |
|  0   | London, England |   1   |
  1. Second would be a similar data frame with country and count.
  2. And, finally the last would show the gender and category against the count.

So far, I've been able to extract the three dictionaries that I eventually need to convert to separate data frames. I've stored the dictionaries in a list all_data.

all_data = []
for item in audience_insight['data']:
    data = item['values'][0]['value']
    all_data.append(data)
df_location = pd.DataFrame(all_data)

all_data

[{'London, England': 1,
  'Kharkiv, Kharkiv Oblast': 1,
  'Jamui, Bihar': 1,
  'Burdwan, West Bengal': 1,
  'Kolkata, West Bengal': 112,
  'Dhulian, West Bengal': 1,
  'Argonne, Wisconsin': 1,
  'College Park, Georgia': 1,
  'Bristol, England': 1,
  'Bikaner, Rajasthan': 1,
  'Delhi, Delhi': 1,
  'Gaya, Bihar': 1,
  'Howrah, West Bengal': 1,
  'Jaipur, Rajasthan': 1,
  'Kanpur, Uttar Pradesh': 1,
  'Panipat, Haryana': 1,
  'Saint Etienne, Rhône-Alpes': 1,
  'Panagarh, West Bengal': 1,
  'Panchagan, Odisha': 1,
  'Bhagalpur, Bihar': 1,
  'Frankfurt, Hessen': 1,
  'Riyadh, Riyadh Region': 1,
  'Roorkee, Uttarakhand': 1,
  'Harinavi, West Bengal': 1,
  'Mumbai, Maharashtra': 3,
  'Patna, Bihar': 11,
  'Obando, Valle del Cauca': 1,
  'Jaunpur, Uttar Pradesh': 1,
  'Hyderabad, Telangana': 1,
  'Sitamau, Madhya Pradesh': 1},
 {'DE': 1, 'IN': 144, 'GB': 2, 'FR': 1, 'CO': 1, 'UA': 1, 'SA': 1, 'US': 2},
 {'F.13-17': 1,
  'F.18-24': 20,
  'F.25-34': 5,
  'M.13-17': 4,
  'M.18-24': 79,
  'M.25-34': 16,
  'M.35-44': 1,
  'M.45-54': 3,
  'U.13-17': 4,
  'U.18-24': 15,
  'U.25-34': 2,
  'U.45-54': 3}]

I want to be able to convert each of these dictionaries into a data frame such that the keys are in the first column and the values are in the second.

Thank you for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source