Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每小时过滤数据_Python_Pandas_Datetime - Fatal编程技术网

Python 每小时过滤数据

Python 每小时过滤数据,python,pandas,datetime,Python,Pandas,Datetime,我有不同的数据集,其中一些数据以5分钟/15分钟或30分钟为间隔。有100个这样的文件(不同格式-.dat、.txt、.csv等) 我想使用Python从所有这些文件中过滤出每小时的数据。我对使用熊猫还不熟悉,在我尝试学习图书馆的时候,任何帮助都会非常感激 Date Time Point_1 27/3/2017 0:00:00 13.08 27/3/2017 0:05:00 12.96 27/3/2017 0:10:00 13.3 27/3/2017 0:15

我有不同的数据集,其中一些数据以5分钟/15分钟或30分钟为间隔。有100个这样的文件(不同格式-.dat、.txt、.csv等) 我想使用Python从所有这些文件中过滤出每小时的数据。我对使用熊猫还不熟悉,在我尝试学习图书馆的时候,任何帮助都会非常感激

Date        Time    Point_1
27/3/2017   0:00:00 13.08
27/3/2017   0:05:00 12.96
27/3/2017   0:10:00 13.3
27/3/2017   0:15:00 13.27
27/3/2017   0:20:00 13.15
27/3/2017   0:25:00 13.14
27/3/2017   0:30:00 13.25
27/3/2017   0:35:00 13.26
27/3/2017   0:40:00 13.24
27/3/2017   0:45:00 13.43
27/3/2017   0:50:00 13.23
27/3/2017   0:55:00 13.27
27/3/2017   1:00:00 13.19
27/3/2017   1:05:00 13.17
27/3/2017   1:10:00 13.1
27/3/2017   1:15:00 13.06
27/3/2017   1:20:00 12.99
27/3/2017   1:25:00 13.08
27/3/2017   1:30:00 13.04
27/3/2017   1:35:00 13.06
27/3/2017   1:40:00 13.07
27/3/2017   1:45:00 13.07
27/3/2017   1:50:00 13.02
27/3/2017   1:55:00 13.13
27/3/2017   2:00:00 12.99

这是一个类似于你要做的事情。这适用于csv文件,也适用于.txt文件。如果所有数据的顺序相同,您可以很容易地编写for循环来增加计数,当它达到13 out时,将该值放入xaxis列表。但是,如果您的数据没有遵循以5分钟为增量的相同模式,那么您需要按照另一个指标对其进行排序,以避免以后的麻烦。不过,使用matplotlib中的pythons排序函数很容易做到这一点

您可以使用参数
parse_dates
将列
date
time
转换为
datetime
首先:

import pandas as pd
from pandas.compat import StringIO

temp=u"""Date        Time    Point_1
27/3/2017   0:00:00 13.08
27/3/2017   0:05:00 12.96
27/3/2017   0:10:00 13.3
27/3/2017   0:15:00 13.27
27/3/2017   0:20:00 13.15
27/3/2017   0:25:00 13.14
27/3/2017   0:30:00 13.25
27/3/2017   0:35:00 13.26
27/3/2017   0:40:00 13.24
27/3/2017   0:45:00 13.43
27/3/2017   0:50:00 13.23
27/3/2017   0:55:00 13.27
27/3/2017   1:00:00 13.19
27/3/2017   1:05:00 13.17
27/3/2017   1:10:00 13.1
27/3/2017   1:15:00 13.06
27/3/2017   1:20:00 12.99
27/3/2017   1:25:00 13.08
27/3/2017   1:30:00 13.04
27/3/2017   1:35:00 13.06
27/3/2017   1:40:00 13.07
27/3/2017   1:45:00 13.07
27/3/2017   1:50:00 13.02
27/3/2017   1:55:00 13.13
27/3/2017   2:00:00 12.99"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), 
                sep="\s+", #alternatively delim_whitespace=True
                index_col=[0], 
                parse_dates={'Dates':['Date','Time']})
然后,求和或求和,
平均值

df1 = df.resample('1H')['Point_1'].first().reset_index()
print (df1)
                Dates  Point_1
0 2017-03-27 00:00:00    13.08
1 2017-03-27 01:00:00    13.19
2 2017-03-27 02:00:00    12.99
另一个解决方案包括和:

或者可能需要:

df = pd.read_csv(StringIO(temp),delim_whitespace=True, parse_dates={'Dates':['Date','Time']})

mask = df.Dates.dt.round('H').ne(df.Dates)
df1 = df[mask]
print (df1)
                 Dates  Point_1
1  2017-03-27 00:05:00    12.96
2  2017-03-27 00:10:00    13.30
3  2017-03-27 00:15:00    13.27
4  2017-03-27 00:20:00    13.15
5  2017-03-27 00:25:00    13.14
6  2017-03-27 00:30:00    13.25
7  2017-03-27 00:35:00    13.26
8  2017-03-27 00:40:00    13.24
9  2017-03-27 00:45:00    13.43
10 2017-03-27 00:50:00    13.23
11 2017-03-27 00:55:00    13.27
13 2017-03-27 01:05:00    13.17
14 2017-03-27 01:10:00    13.10
15 2017-03-27 01:15:00    13.06
16 2017-03-27 01:20:00    12.99
17 2017-03-27 01:25:00    13.08
18 2017-03-27 01:30:00    13.04
19 2017-03-27 01:35:00    13.06
20 2017-03-27 01:40:00    13.07
21 2017-03-27 01:45:00    13.07
22 2017-03-27 01:50:00    13.02
23 2017-03-27 01:55:00    13.13
谢谢大家

这是我从所有文件夹读取所有文件并将过滤数据(仅每小时一次)写入新csv文件的完整代码。 我不经常编写代码,所以我的编程技能不是很好。我确信有更好的方法来做同样的事情,我不仅仅是在谈论pandas库,而是整个代码。我希望我能用更好的东西来代替我的if循环。这主要是为了防止列表超出索引(类似于k=k-1,但不确定放在哪里) 我的代码运行正常。如果有任何“让更好”的爱好者,请加入

我的文件夹结构如下:Building1是主文件夹,包含20个子文件夹,每个子文件夹包含19-20个文件

干杯

import os
import pandas as pd
folderarray = []
filearray =[]
patharray =[]

path = "C:\Users\Priyanka\Documents\R_Python\OneHourInterval\Building1"
os.chdir(path)


for foldername in os.listdir(os.getcwd()):
    folderarray.append(foldername)
    print folderarray

for i in range(0,len(folderarray)):
    filename = os.listdir(path+"\\"+folderarray[i])
    filearray.append(filename)

for j in range(0,len(folderarray)):
    for k in range(0,len(filearray)):
        if k < len(filearray[j]):
            df1 = pd.read_csv(path+"""\\"""+folderarray[j]+"""\\"""+filearray[j][k], sep=",", header=None)
            df = df1[2:len(df1)]
            df = df[[0,1,2,3,4,5]]
            df.columns = ['Date','Time','KWH','OCT','RAT','CO2']
            dftime = pd.to_datetime(df['Time'])    
            df['dt'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
            df = df.set_index('dt').resample('1H')['KWH','OCT','RAT','CO2'].first().reset_index()
            print df
            print path+"""\\"""+folderarray[j]+"""\\"""+filearray[j][k]
            str = filearray[j][k]
            newfilename = str.replace(".dat",".csv")
            df.to_csv(path+"""\\"""+folderarray[j]+"""\\"""+newfilename)
导入操作系统
作为pd进口熊猫
folderarray=[]
filearray=[]
patharray=[]
path=“C:\Users\Priyanka\Documents\R\u Python\OneHourInterval\Building1”
os.chdir(路径)
对于os.listdir(os.getcwd())中的foldername:
folderarray.append(foldername)
印刷折页
对于范围(0,len(folderarray))内的i:
filename=os.listdir(路径+“\\”+folderarray[i])
追加(文件名)
对于范围(0,len(folderarray))内的j:
对于范围(0,len(filearray))中的k:
如果k
Hi,你说的过滤是什么意思?是否要按小时执行聚合?也许是计数?我想我得到了一些意想不到的结果。之前我使用df.resample('1H').last(),从最后一天开始我只得到了几个小时,但现在我尝试使用所有的解决方案,我只得到(1)第一天(24小时)或(2)最后一天(24小时)或(3)第一天和最后一天(48小时)。中间的所有值都由NaN填充。我不知道如何重新采样我所有的数据,而不只是得到第一个或最后一个。我确实有正在重新采样的那些值的数据NaN@jezrael:这是我的数据在重新采样23 2016-01-01 23:00:00 4753.00 15.7 23.5 372.3 25 2016-01-02 01:00:00楠楠楠26 2016-01-02 02:00楠楠楠楠楠楠楠楠楠楠楠楠楠楠楠... ... ... ... ... 南南8037 2016-11-11-30 20:00:00南南8037 2016-11-30 21:00:00南南8038 2016-11-30 22:00:00南南8039 2016-11-30 23:00:00南南8040 2016-12-01 00:00:00 4811.96 14.8 24.8 364.3
df1 = df.resample('1H')['Point_1'].sum().reset_index()
print (df1)
                Dates  Point_1
0 2017-03-27 00:00:00   158.58
1 2017-03-27 01:00:00   156.98
2 2017-03-27 02:00:00    12.99
df1 = df.groupby(pd.Grouper(freq='1H')).first().reset_index()
print (df1)
                Dates  Point_1
0 2017-03-27 00:00:00    13.08
1 2017-03-27 01:00:00    13.19
2 2017-03-27 02:00:00    12.99
df = pd.read_csv(StringIO(temp),delim_whitespace=True, parse_dates={'Dates':['Date','Time']})

mask = df.Dates.dt.round('H').ne(df.Dates)
df1 = df[mask]
print (df1)
                 Dates  Point_1
1  2017-03-27 00:05:00    12.96
2  2017-03-27 00:10:00    13.30
3  2017-03-27 00:15:00    13.27
4  2017-03-27 00:20:00    13.15
5  2017-03-27 00:25:00    13.14
6  2017-03-27 00:30:00    13.25
7  2017-03-27 00:35:00    13.26
8  2017-03-27 00:40:00    13.24
9  2017-03-27 00:45:00    13.43
10 2017-03-27 00:50:00    13.23
11 2017-03-27 00:55:00    13.27
13 2017-03-27 01:05:00    13.17
14 2017-03-27 01:10:00    13.10
15 2017-03-27 01:15:00    13.06
16 2017-03-27 01:20:00    12.99
17 2017-03-27 01:25:00    13.08
18 2017-03-27 01:30:00    13.04
19 2017-03-27 01:35:00    13.06
20 2017-03-27 01:40:00    13.07
21 2017-03-27 01:45:00    13.07
22 2017-03-27 01:50:00    13.02
23 2017-03-27 01:55:00    13.13
import os
import pandas as pd
folderarray = []
filearray =[]
patharray =[]

path = "C:\Users\Priyanka\Documents\R_Python\OneHourInterval\Building1"
os.chdir(path)


for foldername in os.listdir(os.getcwd()):
    folderarray.append(foldername)
    print folderarray

for i in range(0,len(folderarray)):
    filename = os.listdir(path+"\\"+folderarray[i])
    filearray.append(filename)

for j in range(0,len(folderarray)):
    for k in range(0,len(filearray)):
        if k < len(filearray[j]):
            df1 = pd.read_csv(path+"""\\"""+folderarray[j]+"""\\"""+filearray[j][k], sep=",", header=None)
            df = df1[2:len(df1)]
            df = df[[0,1,2,3,4,5]]
            df.columns = ['Date','Time','KWH','OCT','RAT','CO2']
            dftime = pd.to_datetime(df['Time'])    
            df['dt'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
            df = df.set_index('dt').resample('1H')['KWH','OCT','RAT','CO2'].first().reset_index()
            print df
            print path+"""\\"""+folderarray[j]+"""\\"""+filearray[j][k]
            str = filearray[j][k]
            newfilename = str.replace(".dat",".csv")
            df.to_csv(path+"""\\"""+folderarray[j]+"""\\"""+newfilename)