I am trying to make a program that analyzes stocks, and right now I wrote a simple python script to plot moving averages. Extracting the CSV file from the native path works fine, but when I get it from the web, it doesn't work. Keeps displaying an error: 'list' object has no attribute 'Date'
It worked fine with .CSV, but the web thing is messed up. If I run print(df), it displays the table really weirdly.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_html("https://finance.yahoo.com/quote/AAPL/history?period1=1428469200&period2=1554699600&interval=1d&filter=history&frequency=1d")
x = df.Date
y = df.Close
a = df['Close'].rolling(50, min_periods=50).mean()
b = df['Close'].rolling(200, min_periods=200).mean()
plt.plot(x, y)
plt.plot(a)
plt.plot(b)
plt.savefig("AAPL Stuff")
I ran in Jupyter Notebook.
I expected the output out[1] an image of the chart, but I got the error:
AttributeError Traceback (most recent call last)
<ipython-input-18-d97fbde31cef> in <module>
4
5 df = pd.read_html("https://finance.yahoo.com/quote/AAPL/history?period1=1428469200&period2=1554699600&interval=1d&filter=history&frequency=1d")
----> 6 x = df.Date
7 y = df.Close
8
AttributeError: 'list' object has no attribute 'Date'
The data got placed in a (one-element) list.
If you do this, after the read_html
call, it should work:
df = df[0]
Did you mean to access the Date feature from the DataFrame object? If that is the case, then change:
python x = df.Date
to python x = df['Date']
python y = df.Close
to python y = df['Close']
EDIT:
Also: python df.plot(x='Date', y='Close', style='o')
works instead of plt.plot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.