I want to create a dataFrame using a loop function over a list that is composed of dictionary and another list.
list = [{'sitemap': [{'path': 'http://test.com',
'errors': '0',
'contents': [{'type': 'web', 'submitted': '34801', 'indexed': '4656'}]}]},
{'sitemap': [{'path': 'https://example.com',
'errors': '0',
'contents': [{'type': 'web', 'submitted': '2329'}]}]}]
Originally, this is what I tried:
data_for_df = []
for each in list:
temp = []
temp.append(each['sitemap'][0]['path'])
temp.append(each['sitemap'][0]['errors'])
temp.append(each['sitemap'][0]['contents'][0]['type'])
temp.append(each['sitemap'][0]['contents'][0]['submitted'])
temp.append(each['sitemap'][0]['contents'][0]['indexed'])
data_for_df.append(temp)
df = pd.DataFrame(data_for_df, columns =['path','lastSubmitted','type','submitted'])
However, I found that this query returns error because sometimes there are missing key:values. In this example, key:value pairs for 'indexed'is missing. When this happens, I want to return empty or replace it with null value. Could anyone help me with this?