[英]Pandas Data Frame from Web is not Displaying Correctly Compared to the Native CSV File
我正在嘗試制作一個分析股票的程序,現在我編寫了一個簡單的python腳本來繪制移動平均線。 從本機路徑提取CSV文件可以正常工作,但是當我從網絡上獲取它時,它將無法正常工作。 持續顯示錯誤:“列表”對象沒有屬性“日期”
.CSV可以正常工作,但是網絡內容混亂了。 如果我運行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")
我在Jupyter Notebook中跑了。
我期望輸出結果為[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'
數據放入一個(一個元素)列表中。
如果這樣做,則在read_html
調用之后,它應該可以工作:
df = df[0]
您是否要從DataFrame對象訪問Date功能? 如果是這種情況,請更改:
python x = df.Date
到python x = df['Date']
python y = df.Close
python y = df['Close']
編輯:
另外: python df.plot(x='Date', y='Close', style='o')
可以代替plt.plot
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.