I'm trying to make my code leaner
My final aim is to create different dataframes from json urls, using pd.read_json('URLAPI.com/parameter').
Each api url differs just by one endpoint. I'm trying to use a for loop to:
api = ["locations, species"] #creating a list with different endpoint
for i in api:
df_i = pd.read_json(('https://ghibliapi.herokuapp.com/' i)) #create a df with different name
#according to i, and the link is a concatenated string
Unfortunately, I got an error from pandas:
URL can't contain control characters. '/locations, species' (found at least ' ')
I'd like that each df_i it's called df_locations, df_species and for each cicle the link would be something like ...com/locations, ...com/species
Could you please help me? Thanks a lot,
Vincenzo.
CodePudding user response:
Change this
api = ["locations, species"]
to
api = ["locations", "species"]
Secondly, df_i is just one dataframe. You should loop over different dataframes. Create a dictionary of dataframes.
api = ["locations", "species"]
d = {}
for i in api:
d[i] = pd.read_json(('https://ghibliapi.herokuapp.com/' i))
Then, you can access each dataframe with
d["locations"]
d["species"]