看的是这篇文章
https://blog.csdn.net/oh5w6hinug43jvrhhb/article/details/78796069
第一个py文件生成随机数,没有报错
import csv
import random
import datetime
fn='data.csv'
with open(fn,'w',newline='') as fp:
wr=csv.writer(fp)
wr.writerow(['日期','销量'])
startDate=datetime.date(2017,1,1)
for i in range(365):
amount=300+5*1+random.randrange(100)
wr.writerow([str(startDate),amount])
startDate=startDate+datetime.timedelta(days=1)
运行第二个文件的时候,不知道为啥就报错了
代码:
import pandas as pd
import matplotlib.pyplot as plt
#读取数据,丢弃缺失值
df=pd.read_csv('data.csv',encoding='cp936')
df=df.dropna()
#生成营业额折线图
plt.figure()
df.plot(x=df['日期'])
plt.savefig('first.pdf')
KeyError: “None of [Index([‘2017-01-01’, ‘2017-01-02’, ‘2017-01-03’, ‘2017-01-04’, ‘2017-01-05’,\n ‘2017-01-06’, ‘2017-01-07’, ‘2017-01-08’, ‘2017-01-09’, ‘2017-01-10’,\n …\n ‘2017-12-29’, ‘2017-12-30’, ‘2017-12-31’],\n dtype=‘object’, length=365)] are in the [columns]”