2 回答
TA贡献1777条经验 获得超3个赞
看来直接使用 url 作为 df 参数是可行的。你尝试过吗?
import pandas as pd
s_url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?cosd=1990-01-01&coed=2040-01-01&id=CPIAUCSL'
df = pd.read_csv(s_url)
TA贡献1795条经验 获得超7个赞
您可以使用stringIO从字符串中读取数据。
data = b'DATE,CPIAUCSL\n1990-01-01,127.5\n1990-02-01,128.0\n1990-03-01,128.6\n1990-04-01,128.9\n1990-05-01,129.1\n1990-06-01,129.9\n1990-07-01,130.5\n1990-08-01,131.6\n1990-09-01,132.5\n1990-10-01,133.4\n1990-11-01,133.7\n1990-12-01,134.2\n1991-01-01,134.7\n1991-02-01,134.8\n1991-03-01,134.8\n1991-04-01,135.1\n1991-05-01,135.6\n1991-06-01,136.0\n'
from io import StringIO
import pandas as pd
data = data.decode('utf-8') #decode binary string to utf-8
df = pd.read_csv(StringIO(data)) #read the data
df
输出:
DATE CPIAUCSL
0 1990-01-01 127.5
1 1990-02-01 128.0
2 1990-03-01 128.6
3 1990-04-01 128.9
4 1990-05-01 129.1
5 1990-06-01 129.9
添加回答
举报