[英]Pandas Data Frame from Web is not Displaying Correctly Compared to the Native CSV File
I am trying to make a program that analyzes stocks, and right now I wrote a simple python script to plot moving averages. 我正在尝试制作一个分析股票的程序,现在我编写了一个简单的python脚本来绘制移动平均线。 Extracting the CSV file from the native path works fine, but when I get it from the web, it doesn't work.
从本机路径提取CSV文件可以正常工作,但是当我从网络上获取它时,它将无法正常工作。 Keeps displaying an error: 'list' object has no attribute 'Date'
持续显示错误:“列表”对象没有属性“日期”
It worked fine with .CSV, but the web thing is messed up. .CSV可以正常工作,但是网络内容混乱了。 If I run print(df), it displays the table really weirdly.
如果我运行print(df),它会非常奇怪地显示该表。
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. 我在Jupyter Notebook中跑了。
I expected the output out[1] an image of the chart, but I got the error: 我期望输出结果为[1]图表的图像,但出现错误:
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: 如果这样做,则在
read_html
调用之后,它应该可以工作:
df = df[0]
Did you mean to access the Date feature from the DataFrame object? 您是否要从DataFrame对象访问Date功能? If that is the case, then change:
如果是这种情况,请更改:
python x = df.Date
to python x = df['Date']
python x = df.Date
到python x = df['Date']
python y = df.Close
to python y = df['Close']
python y = df.Close
python y = df['Close']
EDIT: 编辑:
Also: python df.plot(x='Date', y='Close', style='o')
works instead of plt.plot 另外:
python df.plot(x='Date', y='Close', style='o')
可以代替plt.plot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.