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

Pandas add rows dynamically via API data fetch

I’m trying to pull data from an API and insert that data into a Pandas dataframe.

I’m retrieving all the necessary data fine but my issue is that each row index isn’t incrementing and only has a row index of 0 for all results, so when I export the data it only shows 1 row of results.

Here is my code. I have the dataframe wrapped in a for loop where the data is coming from:

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

import pandas as pd

for item in response['items']:

    df = pd.DataFrame({
        'Title': [item['snippet']['title']],
        'Description': [item['snippet']['description']],
        'Date Posted': [item['snippet']['publishedAt']],
    })

    print(df)

>Solution :

It’s bad practice to add on to a dataframe. Try something like this:

data = []

for item in response['items']:
    data.append({
        'Title': item['snippet']['title'],
        'Description': item['snippet']['description'],
        'Date Posted': item['snippet']['publishedAt'],
    })

df = pd.DataFrame(data)
print(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