绘制干净的JSON股票价格数据

时间:2020-01-15 18:40:50

标签: python json datetime plot histogram

我想使用提取的数据创建直方图。

x轴应显示月份,垃圾箱应标明价格范围,y轴应显示频率。

直方图的数据框输入如下:

print(three.head())
               Price
Date                
2020-01-15  144.7500
2020-01-14  145.2000
2020-01-13  143.8800
2020-01-10  144.6200
2020-01-09  144.8300 

在尝试绘图之前,我的代码工作正常。当我这样做时,我的函数get_data和clean_data出现了错误-以前工作正常。

Here is my code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 15 08:03:41 2020

    @author: rahelmizrahi
    """
    import json
    import requests
    import statistics 
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt

    def get_data(url):
        request = requests.get(url)
        response = request.text
        data = json.loads(response)
        return data 

    url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=DIS&apikey=IGDV8B3MO9GQ9R35"
    x = get_data(url)

    def clean(data):
        data = get_data(url)

        clean_data = pd.DataFrame(data['Time Series (Daily)']).transpose()

        clean_data = clean_data.drop(columns = ['1. open', '2. high', '3. low', '5. volume'])

        clean_data.reset_index(inplace = True)

        clean_data.rename(columns = {'index':'Date', '4. close': 'Price'}, inplace = True) 

        clean_data.set_index('Date', inplace = True)

        return clean_data

    c = clean(x)

    def prevMonthsData(numMonths, data):
        data = clean(data)
        rows = numMonths * 30
        dataSlice =  data.iloc[0:rows, :]
        return dataSlice


    three = prevMonthsData(3, c)

    # create a histogram

    idx = three.drop(columns = ['Price'])

    three['bucket'] = pd.cut(idx, 10) #gives errror: Input array must be 1 dimensional


    print(idx)

0 个答案:

没有答案