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

How do I convert an array of strings from an API pull to a dataframe with pandas?

With the API I am using, it gives me a list of 15 terms that are ‘trending’ within a given category.

I want to use this data in PowerBI as part of a dashboard, but PowerBI only accepts data in the form of a Pandas dataframe.

I’ve tried a few things but I think the issue comes down to me not understanding how the data is actually being presented to me in the first place to then convert it.

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

The code that pulls from the API:

import requests
import pandas as pd

url = "https://api.newswhip.com/v1/trendingEntities?key=XXX" #APIkey removed for obvious reasons

payload = {
    "filters": ["categories: 33"],
    "from": None,
    "to": None
}
headers = {
    "accept": "application/json",
    "content-type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

The response to which is:

{"trendingEntities":["Charlton","Spanish Super Cup","North London Derby","Bruno Fernandes","Rodrygo","Weghorst","Facundo Pellistri","Pierre-Emerick Aubameyang","North London","Kobbie Mainoo","Angers","Sergio Busquets","FIFA Best","Aaron Ramsdale","Kai Havertz"]}

Ideally, the final output would be:

   0
0  Charlton
1  Spanish Super Cup
2  North London Derby
3  Bruno Fernandes
...
12 FIFA Best
13 Aaron Ramsdale
14 Kai Havertz

Do you have any ideas on how to split the array and convert it to a dataframe? I’ve tried a few things, from blindly asking Pandas to convert the data raw, to using dicts and concats – I didn’t save the code I tried for these, but I’m going to keep working on this and I’ll update the question as I try new methods.

>Solution :

Assuming you’re using the requests library, you can use:

pd.DataFrame(response.json())

This outputs:

             trendingEntities
0                    Charlton
1           Spanish Super Cup
2          North London Derby
3             Bruno Fernandes
4                     Rodrygo
5                    Weghorst
6           Facundo Pellistri
7   Pierre-Emerick Aubameyang
8                North London
9               Kobbie Mainoo
10                     Angers
11            Sergio Busquets
12                  FIFA Best
13             Aaron Ramsdale
14                Kai Havertz
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