I have the following output from an API:
[{'Chapters': {'id': book, 'name': firstbook'}, 'periodPrices': [{'reportDate': '2021-06-01T15:28:00', 'term': '3Q21', 'price': 10.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '4Q21', 'price': 10.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '1Q22', 'price': 10.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '2Q22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '3Q22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '4Q22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '1Q23', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '2Q23', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '2H21', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '1H22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '2H22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': '1H23', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': 'Cal 22', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': 'Cal 23', 'price': 0.0}, {'reportDate': '2021-06-01T15:28:00', 'term': 'Cal 24', 'price': 0.0}]}]
I am trying to get the following output in a dataframe:
Date id Term price
2021- 06-01T00:00:00 book 3Q21 10.0
2021-06-01T00:00:00 book 4Q21 10.0
2021-06-01T00:00:00 book 1Q22 10.5
etc
I tried the following code:
l=parsed ###this is the output from API
df=pd.DataFrame()
for i in l:
d1 = {}
reportDate = []
price = []
for j in i['Chapters']:
reportDate.append(j['Date'])
price.append(j['price'])
d1['Date'] = reportDate
d1['Rate'] = price
df = df.append(pd.DataFrame(d1))
df['Date'] = pd.to_datetime(df['Date'])
However, I get the following error: string indices must be integers for the line for j in i['Chapters']: