gpt4 book ai didi

python - 在 python 中从 CSV 获取特定时间间隔的数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:41 25 4
gpt4 key购买 nike

我正在从 python 中的 CSV 文件获取特定日期的数据。现在,我想获取特定时间的数据,例如特定日期的 13:30 到 14:30。我的 CSV 文件如下所示:

15   2017/02/07  17:30:45.983
15 2017/02/07 17:30:51.109
16 2017/02/07 17:30:56.008
16 2017/02/07 17:31:01.029

我当前的代码是这样的:

    import csv
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo
import datetime
import matplotlib.pyplot as plt





#csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv"))
from Tools.scripts.treesync import raw_input
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)


button1 = Button(self, text="Browse for a file", command=self.askfilename)
button2 = Button(self, text="Count the file", command=self.takedate)
button3 = Button(self, text="Exit", command=master.destroy)
button1.grid()
button2.grid()
button3.grid()
self.userInputFromRaw = Entry(self)
self.userInputFromRaw.grid()

self.userInputToRaw = Entry(self)
self.userInputToRaw.grid()

self.grid()

def askfilename(self):
in_file = askopenfilename()
if not in_file.endswith(('.CSV')):
showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?')
else:
self.in_file=in_file

def CsvImport(self,csv_file):


dist = 0
for row in csv_file:
_dist = row[0]
try:
_dist = float(_dist)
except ValueError:
_dist = 0

dist += _dist
print ("Urine Volume is: %.2f" % (_dist*0.05))


def takedate(self):
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 = ("H:\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






root = Tk()
root.title("Urine Measurement")
root.geometry("500x500")
app = App(root)
root.mainloop()

如何获取特定时间和特定日期的数据?

最佳答案

使用pandas及其DataFrame容器,因为这是处理和选择数据的理想格式。请参阅下面的示例:

import pandas as pd
df = pd.read_csv('eg.txt', header=None) # Read in the Data.
df.index = [pd.datetime.strptime(i, '%Y/%m/%d%H:%M:%S.%f') for i in (df[1] + df[2])] # Format the time into the index

这里

>>> df
0 1 2 3
2017-02-07 17:30:45.983 15 2017/02/07 17:30:45.983 3.3
2017-02-07 17:30:51.109 15 2017/02/07 17:30:51.109 4.4
2017-02-07 17:30:56.008 16 2017/02/07 17:30:56.008 5.2
2017-02-07 17:31:01.029 16 2017/02/07 17:31:01.029 NaN

您可以选择您想要使用的时间范围:

>>> df[pd.datetime(2017, 2, 7, 17, 30, 50):pd.datetime(2017, 2, 7, 17, 30, 58)] # Slice the wanted time
0 1 2 3
2017-02-07 17:30:51.109 15 2017/02/07 17:30:51.109 4.4
2017-02-07 17:30:56.008 16 2017/02/07 17:30:56.008 5.2

其中生成数据的csv是eg.txt,如下所示。

15,2017/02/07,17:30:45.983,3.3
15,2017/02/07,17:30:51.109,4.4
16,2017/02/07,17:30:56.008,5.2
16,2017/02/07,17:31:01.029,NaN

然后您可以根据需要删除、创建、移动列和数据。

关于python - 在 python 中从 CSV 获取特定时间间隔的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42272614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com