如何将json数据框转换为其他数据框

时间:2018-07-14 15:14:15

标签: python python-2.7 pandas

我已经通过json下载了股票数据,我想将此数据转换为其他数据框...我正在使用Python 2.7.15 ...我不是程序员。

我的代码是这样的:

import requests
import pprint
import pandas as pd

url = "https://www.alphavantage.co/query"

function = "TIME_SERIES_DAILY_ADJUSTED" # Date, Open, High, Low, Close,     Volume, Adjusted close, Split/Dividend events

SP500_symbols=['MSFT']

api_key = "XXXXXXXXXXXXXXXX"    #<-- goes here your API KEY

data = { "function": function, 
        "symbol": SP500_symbols, 
        "apikey": api_key } 
page = requests.get(url, params = data)
pprint.pprint(page.json())

我的数据如下:

enter image description here

我想看这样的数据: enter image description here

2 个答案:

答案 0 :(得分:2)

使用 demo-API 键:

import requests
import pandas as pd
import json

url = "https://www.alphavantage.co/query"
params = {
    "function": "TIME_SERIES_DAILY_ADJUSTED",
    "symbol": "MSFT",
    "apikey": "demo"}
response = requests.get(url, params=params)
data = response.json()

def convert_response(d):
    # convert the response into datetimerecords that can be
    # parsed by Pandas
    for dt, prec in d['Time Series (Daily)'].items():
        r = { 'datetime': dt}
        r.update(prec)
        yield r

# pass your response 'page'
df = pd.DataFrame(convert_response(data))
# rename the columns    
df = df.rename(columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. adjusted close': 'AdjClose', '6. volume': 'Volume'})
df['datetime'] = pd.to_datetime(df['datetime'])
df.set_index('datetime', inplace=True)
df.sort_index(inplace=True)
# extract the columns you want
df = df[[ 'Open', 'High', 'Low', 'Close', 'AdjClose', 'Volume']]
print(df)
# or reverse order
# print(df[::-1])

显示:

                Open      High       Low     Close  AdjClose    Volume
datetime                                                              
2018-07-13  104.3700  105.6000  104.0900  105.4300  105.4300  24578301
2018-07-12  102.7700  104.4100  102.7300  104.1900  104.1900  24335929
2018-07-11  101.1500  102.3400  101.1000  101.9800  101.9800  19644648

您可以将它们全部包装在一个函数中。在这种情况下,转换的所有逻辑都被隔离了,您只需调用 df_from_response()

def df_from_response(data,
         columns=['Open', 'High', 'Low', 'Close', 'AdjClose', 'Volume']):

    def convert_response(d):
        # convert the response into datetimerecords that can be
        # parsed by Pandas
        for dt, prec in d['Time Series (Daily)'].items():
            r = { 'datetime': dt}
            r.update(prec)
            yield r

    # pass your response 'page'
    df = pd.DataFrame(convert_response(data))
    # rename the columns    
    df = df.rename(columns={'1. open': 'Open',
                            '2. high': 'High',
                            '3. low': 'Low',
                            '4. close': 'Close',
                            '5. adjusted close': 'AdjClose',
                            '6. volume': 'Volume'})
    df['datetime'] = pd.to_datetime(df['datetime'])
    df.set_index('datetime', inplace=True)
    df.sort_index(inplace=True)
    # extract the default columns
    df = df[columns]
    return df

df = df_from_response(data)
print(df)

答案 1 :(得分:0)

使用Pandas read_json,然后转换Pandas数据框。

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html