如何打印循环迭代的最后一个值

2024-04-23 17:29:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图打印没有相应数据的日期间隔。例如,我想说我没有从2008/04/28 22:00到2008/04/29 00:00以及从2008/10/06 09:45到2008/10/06 10:15的数据记录,等等

这是我文件的一部分:

023004         2008/04/28 22:00                   AR

023004         2008/04/28 22:15                   AR

023004         2008/04/28 22:30                   AR

023004         2008/04/28 22:45                   AR

023004         2008/04/28 23:00                   AR

023004         2008/04/28 23:15                   AR

023004         2008/04/28 23:30                   AR

023004         2008/04/28 23:45                   AR

023004         2008/04/29 00:00    49.37

023004         2008/04/29 00:15    51.41

023004         2008/04/29 00:30    50.96

023004         2008/04/29 00:45    53.73

023004         2008/10/06 09:15    2.587 

023004         2008/10/06 09:30    2.587 

023004         2008/10/06 09:45    2.587 

023004         2008/10/06 10:00                   A

023004         2008/10/06 10:15    2.624

023004         2008/10/06 10:30    2.624

023004         2008/10/06 10:45    2.643

023004         2008/10/06 11:00    2.662

023004         2008/10/06 11:15    2.680

023004         2008/10/06 11:30                   A

023004         2008/10/06 11:45                   A

023004         2008/10/06 12:00                   A

023004         2008/10/06 12:15                   A

023004         2008/10/06 12:30                   A

我试过这个代码:

fich = "test1.txt"

f = open(fich, "rb")
for line in f:
    a = line.split()[3].isalpha()
    if a == False:
        print "valeur"
    else:
        print "Pas de valeur de precipitation du", line.split()[1], "a", line.split()[2], "h ", "au", line.split()[1], line.split()[2], "h "

但它没有给我的价值区间,我正在寻找。它只是告诉我是否有数据。你知道吗

我希望能够打印每个缺失数据间隔的第一个和最后一个值。你知道吗


Tags: 文件数据代码txt间隔记录linede
1条回答
网友
1楼 · 发布于 2024-04-23 17:29:23

这种方法会给出所有没有数据的范围——假设每个数据点之间有一个15分钟的固定步长。它基本上过滤掉没有数据的日期,然后将它们分组成块,每个数据点之间有15分钟的间隔,如果没有,则将下一位数据放入另一块。你知道吗

我将您的示例文本复制并粘贴到excel中,并将其另存为.csv,因此,如果有任何更改,应该可以进行最小程度的更改:

import pandas as pd
import os
delta = pd.Timedelta(15,'m') #define time step
df = pd.read_csv('test.csv',header=0) #read in the data
df['date']=pd.to_datetime(df['date']) #convert the date column to datetime
df = df[pd.notnull(df['date'])] #drop all rows (spaces) with nothing in them
df = df.reset_index(drop=True) #renumber the index

missing_dates=df[df['val'].isnull()]['date'] #dates with no data associated with them
diffs = missing_dates.diff() #difference between missing dates
ranges=[] 
tmp=[]
for i in diffs.index: #loop through the differences
    if pd.isnull(diffs.loc[i]): #first difference always NaT because nothing before it
        tmp.append(missing_dates.loc[i]) #add to temp list
    elif diffs.loc[i] == delta: #if difference is delta, then it is in same chunk as previous data point
        tmp.append(missing_dates.loc[i]) #add to tmp list
    else: #once you reach a data point that is in the next chunk
        ranges.append(tmp) #append temp list to ranges of missing data
        tmp=[] #re-initialize the temp list
        tmp.append(missing_dates.loc[i]) #append value to first position of the list representing the next chunk

ranges.append(tmp)    

这将为您提供一个列表列表,其中每个列表包含没有数据且间隔为1个时间步长的所有时间

但不包括数据缺失日期之前/之后的日期

输出如下所示:

for r in ranges:
    print('No data between '+str(r[0])+' to '+str(r[-1]))

输出:

No data between 2008-04-28 22:00:00 to 2008-04-28 23:45:00
No data between 2008-10-06 10:00:00 to 2008-10-06 10:00:00
No data between 2008-10-06 11:30:00 to 2008-10-06 12:30:00

可能不是最好的方法,但希望你的目标是有帮助的方向

相关问题 更多 >