【问题标题】:Read data from json link and download that to pandas dataframe从 json 链接读取数据并将其下载到 pandas 数据框
【发布时间】:2020-04-18 05:12:00
【问题描述】:

我有一个json链接如下:

网址 = "https://www.nseindia.com/api/live-analysis-variations?index=gainers"

如何将这些数据下载到 pandas 数据框。

【问题讨论】:

  • 获取 URL 时出现 405 Method not allowed
  • 先生,我现在更改了链接,因为之前的链接有一些错误。

标签: python json python-3.x pandas dataframe


【解决方案1】:

这就是我将所有数据获取到 DataFrame 的方式。我添加了一个名为“legend”的新列,如果需要,您可以单独查看每个数据:

import pandas as pd
import requests

# needs header
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '\
                         'AppleWebKit/537.36 (KHTML, like Gecko) '\
                         'Chrome/75.0.3770.80 Safari/537.36'}


URI = 'https://www.nseindia.com/api/live-analysis-variations?index=gainers'
# since data is returned as json, we can use .json func
data = requests.get(URI, headers=headers).json()


print(data['legends'])

# each legend carries data, so we will append all data and add col legend
dfs = pd.DataFrame([])
for legend, _ in data['legends']:

    df = pd.DataFrame(data[legend]['data'])
    df['legend'] = legend
    dfs = dfs.append(df, ignore_index=True)


print(dfs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 2020-05-27
    • 2019-04-09
    • 2021-11-22
    • 1970-01-01
    • 2020-11-06
    • 2014-10-29
    • 2022-06-13
    相关资源
    最近更新 更多