2017-07-10 44 views
-3

我似乎無法使用下面的代碼繪製圖形。我想繪製一張圖表,其中包括日期的總收益。我的代碼有什麼問題?從CSV導入的數據似乎無法在PYTHON中繪圖

我得到KeyError異常: '日期'

電子表格呈現以下

import pandas as pd 
import numpy as np 
from pandas_datareader import data 
import matplotlib.pyplot as plt 

# import data from csv into a dataframe 

sp500 = pd.read_csv(r'C:\Users\James\SPX.csv', index_col = 'Date') 

print sp500.tail() 

#test plot to see if it works 

x = sp500['Date'] 
y = sp500['Total Return'] 


plt.plot(x,y) 

數據是在下面的格式:

  Cash Close Total Return Futures Open Futures Close Futures High \ 
Date 
23-Dec-16 2,263.79  3,052.05  2,257.50  2,260.00  2,261.00 
27-Dec-16 2,268.88  3,058.94  2,258.75  2,261.00  2,269.50 
28-Dec-16 2,249.92  3,033.90  2,261.25  2,245.25  2,267.50 
29-Dec-16 2,249.26  3,033.16  2,245.50  2,245.00  2,250.00 
30-Dec-16 2,238.83  3,019.24  2,246.25  2,236.25  2,252.75 

的相關數據在下面

Date,Cash Close,Total Return,Futures Open,Futures Close,Futures High,Futures Low,Risk Free Rate 
2-Jan-01," 1,283.27 "," 1,283.27 "," 1,331.00 "," 1,299.80 "," 1,336.75 "," 1,289.25 ",6.50% 
3-Jan-01," 1,347.56 "," 1,347.56 "," 1,301.00 "," 1,359.20 "," 1,367.25 "," 1,288.75 ",6.00% 
4-Jan-01," 1,333.34 "," 1,333.34 "," 1,360.00 "," 1,348.30 "," 1,365.50 "," 1,341.50 ",6.00% 
5-Jan-01," 1,298.35 "," 1,298.35 "," 1,345.75 "," 1,304.50 "," 1,380.00 "," 1,303.00 ",6.00% 
8-Jan-01," 1,295.86 "," 1,295.86 "," 1,308.25 "," 1,309.50 "," 1,312.50 "," 1,287.50 ",6.00% 
9-Jan-01," 1,300.80 "," 1,300.80 "," 1,312.00 "," 1,309.50 "," 1,325.50 "," 1,303.75 ",6.00% 
10-Jan-01," 1,313.27 "," 1,313.27 "," 1,309.50 "," 1,327.00 "," 1,328.75 "," 1,297.25 ",6.00% 
11-Jan-01," 1,326.82 "," 1,326.82 "," 1,325.75 "," 1,334.50 "," 1,343.50 "," 1,319.50 ",6.00% 
12-Jan-01," 1,318.32 "," 1,318.32 "," 1,332.00 "," 1,330.30 "," 1,344.75 "," 1,322.00 ",6.00% 
16-Jan-01," 1,326.65 "," 1,326.65 "," 1,328.75 "," 1,335.50 "," 1,339.50 "," 1,323.50 ",6.00% 
17-Jan-01," 1,329.47 "," 1,329.47 "," 1,337.25 "," 1,341.70 "," 1,357.75 "," 1,334.50 ",6.00% 
18-Jan-01," 1,347.97 "," 1,347.97 "," 1,342.50 "," 1,356.00 "," 1,364.00 "," 1,337.00 ",6.00% 
19-Jan-01," 1,342.54 "," 1,342.54 "," 1,359.00 "," 1,348.50 "," 1,371.75 "," 1,345.75 ",6.00% 
22-Jan-01," 1,342.90 "," 1,342.90 "," 1,346.00 "," 1,358.50 "," 1,364.00 "," 1,341.25 ",6.00% 
23-Jan-01," 1,360.40 "," 1,360.40 "," 1,356.75 "," 1,366.00 "," 1,372.75 "," 1,348.50 ",6.00% 
24-Jan-01," 1,364.30 "," 1,364.30 "," 1,366.25 "," 1,370.20 "," 1,379.00 "," 1,365.25 ",6.00% 
25-Jan-01," 1,357.51 "," 1,357.51 "," 1,369.75 "," 1,364.50 "," 1,378.75 "," 1,362.75 ",6.00% 
26-Jan-01," 1,354.95 "," 1,354.95 "," 1,364.25 "," 1,363.30 "," 1,366.75 "," 1,350.25 ",6.00% 
29-Jan-01," 1,364.17 "," 1,364.17 "," 1,363.00 "," 1,370.00 "," 1,374.00 "," 1,358.50 ",6.00% 
30-Jan-01," 1,373.73 "," 1,373.73 "," 1,370.25 "," 1,382.00 "," 1,384.25 "," 1,363.25 ",6.00% 
31-Jan-01," 1,366.01 "," 1,366.01 "," 1,381.00 "," 1,372.90 "," 1,394.00 "," 1,371.25 ",5.50% 
1-Feb-01," 1,373.47 "," 1,373.47 "," 1,397.90 "," 1,397.90 "," 1,397.90 "," 1,397.90 ",5.50% 
2-Feb-01," 1,349.47 "," 1,349.47 "," 1,368.30 "," 1,368.30 "," 1,368.30 "," 1,368.30 ",5.50% 
+0

不知道你的問題/問題是什麼。另外:在將來,請注意代碼格式。正如最初發布的那樣,它是不可讀的(我將其格式化)。 –

+0

感謝哥們,讓我也重新編輯這個問題。乾杯。 – james

+0

你正在運行jupyter筆記本或jupyter QT控制檯嗎? – ksai

回答

1

你有問題就在這裏啓動,在此您創建CSV文件中的點。該文件的一個行看起來像這樣

2-Jan-01," 1,283.27 "," 1,283.27 "," 1,331.00 "," 1,299.80 "," 1,336.75 "," 1,289.25 ",6.50% 

正如你所看到的,使用的是逗號(,)作爲字段分隔以及爲千位分隔符。這使得必須將字段值封裝爲引號(")。反過來,這導致大熊貓需要將字段讀作字符串而不是數字。

一個選項當然會在創建時更改文件的csv格式。如果這不可行,您需要使用pandas.read_csv提供的一些選項來正確讀取數據。

sp500 = pd.read_csv('spx1.csv', index_col = 'Date', 
        parse_dates=True, thousands=r",", quotechar='"') 

現在出現第二個問題:您沒有「日期」列。 「日期」是數據框的索引,而不是列(因爲您使用的是index_col = 'Date')。因此,你需要使用這個指標的值x被繪製,

x = sp500.index 

一個完整的例子:

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

# import data from csv into a dataframe 
sp500 = pd.read_csv('spx1.csv', index_col = 'Date', parse_dates=True, 
        thousands=r",", quotechar='"') 

x = sp500.index 
y = sp500['Total Return'] 

plt.plot(x,y) 

plt.show() 

enter image description here

+0

非常感謝您,您不知道這對我和我對python/pandas的緩慢理解有多大幫助。 歡呼你的偉大答案。 – james

0

嘗試這樣的:

import matplotlib.pyplot as plt 
import pandas as pd 
df = pd.read_csv('spx1.csv', index_col = 'Date') 
df.index = pd.to_datetime(df.index) #convert index to datetime 
x = list(df.index.values) # get index list 
y = [float(e.strip().replace(',','')) for e in df['Total Return'].tolist()] #convert to float and get as list 
plt.plot(x,y) 
plt.show() 

,它會顯示:

enter image description here

+0

工程就像一個魅力。你的幫助! – james

+0

@james cheers :)如果有幫助,不要忘了標記爲接受答案:) –

+0

當然。如果你不介意幫助,你能解釋爲什麼我必須操縱數據,轉換成浮動,然後列表才能繪製? – james