将json数据下载到df中?

2024-09-30 20:30:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从以下位置下载json数据:link

json数据分为“pjtj:”、“jgyc:”、“mgsy:”……等类别

我的代码返回:

ValueError: arrays must all be same length

如何仅提取“mgsy:”类别下的数据

我的代码:

url = "http://emweb.securities.eastmoney.com/ProfitForecast/ProfitForecastAjax?code=SZ002439"
df = pd.read_json(url)  
print(df)

Tags: 数据代码jsonurldflinkbeall
2条回答

您可以尝试使用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信息的数据帧。希望它能帮助你

一种选择是使用requestsjson下载并提取json数据,然后将其转换为pandas{}:

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

相关问题 更多 >