SkuPak
SkuPak

Reputation: 367

Loop for creating a single dataframe, starting from multiple json

I'd like to loop the following lines:

#convert json to a dataframe
aaa = pd.DataFrame(response2[0].json()['result']['tags'])

#adds a new col (same values for each row)
aaa['image'] = image_url[0]

My attempt:

for i in range(len(response2)):
    aaa = pd.DataFrame(response2[i].json()['result']['tags'])
    aaa['image'] = image_url[i]

Obviously this works but overwrites aaa each time. The idea is to append the rows to a dataframe but at the same time i need to add the image column with the right i for each call. Thanks

Upvotes: 0

Views: 40

Answers (1)

Florian H
Florian H

Reputation: 3082

aaa = []
for i in range(len(response2)):
    aaa.append(pd.DataFrame(response2[i].json()['result']['tags']))
    aaa[i]['image'] = image_url[i]

aaa = pd.concat(aaa, axis=0)

To specify how to concatenate your date see:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

Upvotes: 1

Related Questions