我编写了一个python程序,它在python中获取特定日期的csv数据。我想在特定时间获取特定时间的数据,例如2小时。但问题是在我的csv文件中,日期和时间存储在不同的列中(col 2表示时间)。我还想在图表上绘制数据。继承我的代码:
from_raw = self.userInputFromRaw.get()
from_date = datetime.date(*map(int, from_raw.split('/')))
print ('From date: = ' + str(from_date))
to_raw = self.userInputToRaw.get()
to_date = datetime.date(*map(int, to_raw.split('/')))
in_file = 'C:\\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.CSV'
in_file= csv.reader(open(in_file,"r"))
for line in in_file:
_dist = line[0]
try:
file_date = datetime.date(*map(int, line[1].split(' ')[1].split('/')))
if from_date <= file_date <= to_date:
self.CsvImport(in_file)
except IndexError:
pass
有人能告诉我如何在特定日期(由用户拍摄)获取特定时间间隔的数据并将其绘制在图表上?我不知道如何同时提取日期和时间。
我的Csv文件如下所示:
2 2017/02/17 23:02:31.615
1 2017/02/17 23:02:36.611
1 2017/02/17 23:02:41.601
2 2017/02/17 23:02:46.748
2 2017/02/17 23:02:51.620
2 2017/02/17 23:02:56.627
1 2017/02/17 23:03:01.617
2 2017/02/17 23:03:06.646
2 2017/02/17 23:03:11.643
答案 0 :(得分:0)
我会使用pandas模块来处理这里的所有内容。这就是我要做的事。
2 2017/02/17 23:02:31.615
1 2017/02/17 23:02:36.611
1 2017/02/17 23:02:41.601
2 2017/02/17 23:02:46.748
2 2017/02/17 23:02:51.620
2 2017/02/17 23:02:56.627
1 2017/02/17 23:03:01.617
2 2017/02/17 23:03:06.646
2 2017/02/17 23:03:11.643
import pandas as pd
import matplotlib.pyplot as plt
filename = "dat.csv"
# arguements for pandas.read_csv
kwargs = {
"sep": "\s+", # specifices that it's a space separeted file
"names": ["value", "date", "time"], # names the columns
"parse_dates": [[1,2]], # combine columns 2 and 3 into a datetime col
"index_col": "date_time", # set the datetime column as the index
}
# read the csv into a pandas dataframe
df = pd.read_csv(filename, **kwargs)
# select a certain range of times
# pandas is smart about slicing date ranges out of a datetime index
# just write out a slice like below using a standard datetime format
# and pandas will figure out what you want. Convenient.
df = df["2017-02-17 23:02:31":"2017-02-17 23:02:51"]
# plot it using pandas built in wrapper for matplotlib.pyplot
df.plot()
# show the plot
plt.show()