[英]Download json data into Pandas df?
我正在尝试从以下位置下载 json 数据: link 。
json 数据分为以下几类:"pjtj:"、"jgyc:"、"mgsy:"...等。
我的代码返回:
ValueError:数组的长度必须相同
如何只提取“mgsy:”类别下的数据?
我的代码:
url = "http://emweb.securities.eastmoney.com/ProfitForecast/ProfitForecastAjax?code=SZ002439"
df = pd.read_json(url)
print(df)
您可以尝试使用带有 json 的中间步骤,这是一个示例:
import json
import pandas as pd
x = {
"name": "John",
"age": 30,
"city": "New York"
}
y = json.dumps(x)
a = pd.read_json(y, orient = 'index')
这将返回一个包含 json 信息的数据帧。 希望能帮到你
一种选择是使用requests
和json
来下载并解压json
数据,然后将其转换为pandas
DataFrame
算账:
import requests
import json
r = requests.get("http://emweb.securities.eastmoney.com/ProfitForecast/ProfitForecastAjax?code=SZ002439")
d = json.loads(r.text)
df = pd.DataFrame(d["mgsy"])
print(df)
>>> ratio value year
0 25.91 0.63 2018A
1 25.69 0.80 2019E
2 26.26 1.01 2020E
3 25.65 1.27 2021E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.