简体   繁体   中英

python - import data from url to pandas

Can you please help me to code the import the data coming from this url into a pandas dataframe? Is a time serie of a mutual fund and I need to make some statistical analysis and plot.

http://tools.morningstar.it/api/rest.svc/timeseries_price/jbyiq3rhyf?currencyId=EURtype=Morningstar&frequency=daily&startDate=2008-04-01&priceType=&outputType=COMPACTJSON&id=F00000YU62]2]0]FOITA $$ALL&applyTrackRecordExtension=true

Any hint is appreciated to understand how it works

thanks

Answer to get result:

import requests
import pandas as pd
URL ='http://tools.morningstar.it/api/rest.svc/timeseries_price/jbyiq3rhyf?currencyId=EURtype=Morningstar&frequency=daily&startDate=2008-04-01&priceType=&outputType=COMPACTJSON&id=F00000YU62]2]0]FOITA$$ALL&applyTrackRecordExtension=true'
r = requests.get(URL)
# a= eval(r.content) Never user eval for online texts
df = pd.DataFrame(r.json())

Answer to understand whats going on

In my answer, I use a little trick that is not recommended all the times. First, I used request to get data from URL and then evaluate it using python eval function, as you can see its a nested list. But its a better idea to use r.json()

pandas.DataFrame is a method that converts data to data frame using different method for example you can use nested list or json like data(like dictionaries) to create a Dataframe.

But In most case results from web can become a pandas Dataframe using pd.read_csv it parse data using sep and lineterminator .

Or you can just use pd.read_json(URL)

import pandas as pd
URL = "http://your.url.com/api"
pd.read_json(URL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM