【发布时间】:2021-04-25 17:16:12
【问题描述】:
我有一个 csv 文件,其日期格式为 M/D/YYYY 从 1948 年到 2017 年。我可以通过列表索引绘制与每个日期关联的其他列/列表。我希望能够向用户询问开始日期和结束日期,然后仅返回/绘制该期间内的数据。
问题是,从 csv 中读取日期,它们是字符串,所以我不能使用 if date[x] >= startDate && date[x] <= endDate,因为我无法将这种格式的日期转换为整数。
我已经能够将 csv 中的日期读取到它自己的列表中。
如何获取列表中的日期并仅返回用户指定日期范围内的日期?
这是我现在绘制整个数据集的函数:
#CSV Plotting function
def CSV_Plot (data,header,column1,column2):
#pyplot.plot([item[column1] for item in data] , [item[column2] for item in data])
pyplot.scatter([item[column1] for item in data] , [item[column2] for item in data])
pyplot.xlabel(header[column1])
pyplot.ylabel(header[column2])
pyplot.show()
return True
CSV_Plot(mycsvdata,data_header,dateIndex,rainIndex)
这就是我要求用户输入开始和结束日期的方式:
#Ask user for start date in M/D/YYY format
startDate = input('Please provide the start date (M/D/YYYY) of the period for the data you would like to plot: ')
endDate = input('Please provide the end date (M/D/YYYY) of the period for the data you would like to plot: ')
【问题讨论】:
标签: python csv date matplotlib plot