Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add JSON from URL to DataFrame in a loop

I’m trying to download JSON data and add it to a DataFrame, but I’m limited to 10 results per request.

The Loop and Offset works, but I can’t get it to add the new data to the DataFrame, it replaces it each time.

I’ve tried using a df2 df2 = df.append(df) and appending the df that’s created each loop, but that’s not working either.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

offset = 10
while offset < 1000:
    url = f"https://someurl/?limit=10&offset={offset}"
    data = pd.read_json(url)
    df = pd.json_normalize(data['results'])
    offset = offset + 10
    
    df = df.append(df)
    print(df)

>Solution :

See below.
The idea is to collect the df into list and to concat list items.
Note that code cant be tested since the URL is a dummy one

import pandas as pd

offset = 10
df_list = []
while offset < 100:
  data = pd.read_json(f"https://someurl/?limit=10&offset={offset}")
  df_list.append(pd.json_normalize(data['results']))
  offset = offset + 10
merged_df = pd.concat(df_list)
print(merged_df)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading