如何打印循环迭代的最后一个值
How to print last value of an iteration for loop
我正在尝试打印没有相应数据的日期间隔。例如,我想说我没有从 2008/04/28 22:00 到 2008/04/29 00:00 和从 2008/10/06 [=24] 记录的数据=] 到 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 "
但它并没有给我我正在寻找的价值区间。它只是告诉我是否有数据。
我希望能够打印每个缺失数据区间的第一个和最后一个值。
这种方法会给你所有没有数据的范围——假设每个数据点之间有一个恒定的 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 个时间步长的所有时间
但是它不会包括日期before/after缺少数据的日期
输出如下:
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
可能不是目前最好的方法,但希望能为您提供帮助的方向
我正在尝试打印没有相应数据的日期间隔。例如,我想说我没有从 2008/04/28 22:00 到 2008/04/29 00:00 和从 2008/10/06 [=24] 记录的数据=] 到 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 "
但它并没有给我我正在寻找的价值区间。它只是告诉我是否有数据。
我希望能够打印每个缺失数据区间的第一个和最后一个值。
这种方法会给你所有没有数据的范围——假设每个数据点之间有一个恒定的 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 个时间步长的所有时间
但是它不会包括日期before/after缺少数据的日期
输出如下:
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
可能不是目前最好的方法,但希望能为您提供帮助的方向