在Python中从CSV获取特定日期和时间的数据

问题描述 投票:0回答:1

我编写了一个 python 程序,它在 python 中获取特定日期的 csv 数据。我还想获取特定时间的数据,例如特定日期 2 小时的数据。但问题是,在我的 csv 文件中,日期和时间存储在不同的列中(第 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
python csv
1个回答
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"
# arguments for pandas.read_csv
kwargs = {
    "sep": "\s+", # specifies that it's a space separated 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()
© www.soinside.com 2019 - 2024. All rights reserved.