我正在尝试根据日期时间索引创建一个简单的折线图。但我收到一条错误消息。
#standard packages
import numpy as np
import pandas as pd
#visualization
%matplotlib inline
import matplotlib.pylab as plt
#create weekly datetime index
edf = pd.read_csv('C:\Users\j~\raw.csv', parse_dates=[6])
edf2 = edf[['DATESENT','Sales','Traffic']].copy()
edf2['DATESENT']=pd.to_datetime(edf2['DATESENT'],format='%m/%d/%Y')
edf2 = edf2.set_index(pd.DatetimeIndex(edf2['DATESENT']))
edf2.resample('w').sum()
edf2
#output
SALES
DATESENT
2014-01-05 476
2014-01-12 67876
然后我尝试绘制(最简单的线图可能按周查看销售额)
#linegraph
edf3.plot(x='DATESENT',y='Sales')
但我收到此错误消息
KeyError:'DATESENT'
答案 0 :(得分:2)
您获得KeyError
因为'DATESENT'
是索引,而不是edf3
中的列。你可以这样做:
#linegraph
edf3.plot(x=edf3.index,y='Sales')