0

Hope you can help.

I need to transform my request data into dataframes, but have some problems.

My code look like this so far:

import requests
import json
from pandas.io.json import json_normalize

url = "https://api-football-v1.p.rapidapi.com/v2/teams/league/442"

headers = {
  'x-rapidapi-host': "api-football-v1.p.rapidapi.com",
  'x-rapidapi-key': "My-rapidapikey-Not-Showing"
}

dict_train = requests.request("GET", url, headers=headers).json()

train = pd.DataFrame.from_dict(dict_train, orient='index')
train.reset_index(level=0, inplace=True)

train

It gives me this.

enter image description here

The data in text, looks like this.

enter image description here

How can I transform my data into dataframes with the right column names? For this it would be "name", "code", "flag", "country" and so on, and make it flexible when I call the api with different requests?

I tried to follow some of the things from here, without success: JSON to pandas DataFrame

Thank you!

Logica
  • 977
  • 4
  • 16
Kasper
  • 121
  • 6

1 Answers1

0

The response that you present in the text data lock like a JSON with "api" key that has "results" and "teams" as values so to get a DataFrame with the columns that you specify you should run this command train = pd.DataFrame.from_dict(dict_train["api"]["teams"]) that will parse the "teams" lists into DataFrame

Leo Arad
  • 4,452
  • 2
  • 6
  • 17
  • Thank you for the answer! But I am getting this error ----> JSONDecodeError: Expecting value: line 1 column 1 (char 0). Is it because it cant read any information ? – Kasper Mar 22 '20 at 14:31
  • I believe it is happening due to the \u00 chars that you have in the values if you will remove them and then parse the "teams" list it may work – Leo Arad Mar 22 '20 at 17:51
  • Thank you for your answer. Could you elaborate a bit on why? Do you the easiest way to remove them from the list? :) – Kasper Mar 22 '20 at 19:26
  • which python version you are using? and can you upload the response in a file? – Leo Arad Mar 24 '20 at 09:49