I'm trying to put data retrieved via an API into a pandas dataframe. I managed to get the data but I'm having issues in converting json into a pandas dataframe. This is the code that I have now:
import requests
import pandas as pd
r = requests.get('https://pokeapi.co/api/v2/pokemon?limit=1200')
data = r.text
df = pd.DataFrame.from_dict(data)
I tried to generate a dataframe but I get the error:
ValueError: DataFrame constructor not properly called!
CodePudding user response:
Use r.json()
instead r.text
:
data = r.json()
df = pd.DataFrame(data['results'])
print(df)
# Output
name url
0 bulbasaur https://pokeapi.co/api/v2/pokemon/1/
1 ivysaur https://pokeapi.co/api/v2/pokemon/2/
2 venusaur https://pokeapi.co/api/v2/pokemon/3/
3 charmander https://pokeapi.co/api/v2/pokemon/4/
4 charmeleon https://pokeapi.co/api/v2/pokemon/5/
... ... ...
1121 copperajah-gmax https://pokeapi.co/api/v2/pokemon/10224/
1122 duraludon-gmax https://pokeapi.co/api/v2/pokemon/10225/
1123 urshifu-single-strike-gmax https://pokeapi.co/api/v2/pokemon/10226/
1124 urshifu-rapid-strike-gmax https://pokeapi.co/api/v2/pokemon/10227/
1125 toxtricity-low-key-gmax https://pokeapi.co/api/v2/pokemon/10228/
[1126 rows x 2 columns]