【问题标题】:How to print last value of an iteration for loop如何打印循环迭代的最后一个值
【发布时间】:2019-12-22 21:24:34
【问题描述】:

我正在尝试打印没有相应数据的日期间隔。例如,我想说我没有从 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 "

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

我希望能够打印每个缺失数据区间的第一个和最后一个值。

【问题讨论】:

  • 如果你把它带到一个数据框,并用每小时拆分重新采样,或者创建另一个索引值列表以左连接你当前的数据,你会得到给定日期时间的 NaN 值您想要显示的值没有任何数据。
  • 我不需要查看给定日期时间的 nan 值,我想知道时间间隔(打印:您从 2008/04/28 22:00 到没有任何数据2008/04/29 00:00 和从 2008/10/06 09:45 到 2008/10/06 10:15 等。我想说:逐行读取文件,如果没有值:打印第一个非值对应的第一个日期,一直持续到有值。然后打印我没有任何数据的对应日期。
  • 我相信这更容易,因为当你得到 NaN 时,你可以 groupby 然后调用 minmax 作为你的间隔,所以你的最小值是 2008/04/28 22:00 和最大值 @ 987654327@这是我诚实的建议,因为我相信我会这样处理这种情况。编辑:不一定是最好的或唯一的方法,但它可能会完成工作。

标签: python file loops for-loop nan


【解决方案1】:

这种方法将为您提供没有数据的所有范围 - 假设每个数据点之间有一个恒定的 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

可能不是目前最好的方法,但希望能帮助你朝着一个有帮助的方向前进

【讨论】:

  • 谢谢 Derek Eden 成功了。我只需要在您的代码中添加一件事。我将我的数据列转换为数值: df['Data'] = pd.to_numeric(df['Data'], errors='coerce') 因为 A 字母和值已合并
  • 很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多