[英]Find index of consecutive values within a dataframe when number of consecutive values is below a certain threshold
我有一个如下所示的数据框:
night DSWRF_integ
ForecastTime
2018-05-12 00:00:00 1.0 1
2018-05-12 00:15:00 0.0 1
2018-05-12 00:30:00 0.0 1
2018-05-12 00:45:00 0.0 1
2018-05-12 01:00:00 0.0 0
2018-05-12 01:15:00 0.0 0
2018-05-12 01:30:00 0.0 0
2018-05-12 01:45:00 0.0 0
2018-05-12 02:00:00 0.0 0
2018-05-12 02:15:00 0.0 0
2018-05-12 02:30:00 0.0 0
2018-05-12 02:45:00 0.0 0
2018-05-12 03:00:00 0.0 0
2018-05-12 03:15:00 0.0 0
2018-05-12 03:30:00 0.0 0
2018-05-12 03:45:00 0.0 0
2018-05-12 04:00:00 0.0 0
2018-05-12 04:15:00 0.0 0
2018-05-12 04:30:00 0.0 0
2018-05-12 04:45:00 0.0 0
2018-05-12 05:00:00 0.0 0
2018-05-12 05:15:00 0.0 0
2018-05-12 05:30:00 0.0 0
2018-05-12 05:45:00 0.0 0
2018-05-12 06:00:00 0.0 0
2018-05-12 06:15:00 0.0 0
2018-05-12 06:30:00 0.0 0
2018-05-12 06:45:00 0.0 0
2018-05-12 07:00:00 0.0 0
2018-05-12 07:15:00 0.0 0
2018-05-12 07:30:00 0.0 0
2018-05-12 07:45:00 0.0 0
2018-05-12 08:00:00 0.0 0
2018-05-12 08:15:00 0.0 0
2018-05-12 08:30:00 0.0 0
2018-05-12 08:45:00 0.0 0
2018-05-12 09:00:00 0.0 0
2018-05-12 09:15:00 0.0 0
2018-05-12 09:30:00 0.0 0
2018-05-12 09:45:00 0.0 0
2018-05-12 10:00:00 0.0 0
2018-05-12 10:15:00 0.0 0
2018-05-12 10:30:00 0.0 0
2018-05-12 10:45:00 0.0 0
2018-05-12 11:00:00 0.0 0
2018-05-12 11:15:00 0.0 1
2018-05-12 11:30:00 0.0 1
2018-05-12 11:45:00 0.0 1
2018-05-12 12:00:00 0.0 0
2018-05-12 12:15:00 0.0 0
2018-05-12 12:30:00 0.0 0
2018-05-12 12:45:00 0.0 0
2018-05-12 13:00:00 0.0 0
2018-05-12 13:15:00 0.0 0
2018-05-12 13:30:00 0.0 0
2018-05-12 13:45:00 0.0 0
2018-05-12 14:00:00 1.0 1
2018-05-12 14:15:00 1.0 1
2018-05-12 14:30:00 1.0 1
2018-05-12 14:45:00 1.0 1
2018-05-12 15:00:00 1.0 1
我试图找出一个逻辑,而不是迭代数据帧,因为它太慢,能够将列DSWRF_integ中的连续零转换为1, 只有当连续零的数量小于特定阈值时(例如阈值= 10)。
在这个特定的情况下,我想将列DSWRF_integ中的所有零替换为1,时间段为2018-05-12 12:00:00到2018-05-12 13:45:00 ,因为数量为连续零小于10。
生成的数据框应如下所示:
night DSWRF_integ
ForecastTime
2018-05-12 00:00:00 1.0 1
2018-05-12 00:15:00 0.0 1
2018-05-12 00:30:00 0.0 1
2018-05-12 00:45:00 0.0 1
2018-05-12 01:00:00 0.0 0
2018-05-12 01:15:00 0.0 0
2018-05-12 01:30:00 0.0 0
2018-05-12 01:45:00 0.0 0
2018-05-12 02:00:00 0.0 0
2018-05-12 02:15:00 0.0 0
2018-05-12 02:30:00 0.0 0
2018-05-12 02:45:00 0.0 0
2018-05-12 03:00:00 0.0 0
2018-05-12 03:15:00 0.0 0
2018-05-12 03:30:00 0.0 0
2018-05-12 03:45:00 0.0 0
2018-05-12 04:00:00 0.0 0
2018-05-12 04:15:00 0.0 0
2018-05-12 04:30:00 0.0 0
2018-05-12 04:45:00 0.0 0
2018-05-12 05:00:00 0.0 0
2018-05-12 05:15:00 0.0 0
2018-05-12 05:30:00 0.0 0
2018-05-12 05:45:00 0.0 0
2018-05-12 06:00:00 0.0 0
2018-05-12 06:15:00 0.0 0
2018-05-12 06:30:00 0.0 0
2018-05-12 06:45:00 0.0 0
2018-05-12 07:00:00 0.0 0
2018-05-12 07:15:00 0.0 0
2018-05-12 07:30:00 0.0 0
2018-05-12 07:45:00 0.0 0
2018-05-12 08:00:00 0.0 0
2018-05-12 08:15:00 0.0 0
2018-05-12 08:30:00 0.0 0
2018-05-12 08:45:00 0.0 0
2018-05-12 09:00:00 0.0 0
2018-05-12 09:15:00 0.0 0
2018-05-12 09:30:00 0.0 0
2018-05-12 09:45:00 0.0 0
2018-05-12 10:00:00 0.0 0
2018-05-12 10:15:00 0.0 0
2018-05-12 10:30:00 0.0 0
2018-05-12 10:45:00 0.0 0
2018-05-12 11:00:00 0.0 0
2018-05-12 11:15:00 0.0 1
2018-05-12 11:30:00 0.0 1
2018-05-12 11:45:00 0.0 1
2018-05-12 12:00:00 0.0 1
2018-05-12 12:15:00 0.0 1
2018-05-12 12:30:00 0.0 1
2018-05-12 12:45:00 0.0 1
2018-05-12 13:00:00 0.0 1
2018-05-12 13:15:00 0.0 1
2018-05-12 13:30:00 0.0 1
2018-05-12 13:45:00 0.0 1
2018-05-12 14:00:00 1.0 1
2018-05-12 14:15:00 1.0 1
2018-05-12 14:30:00 1.0 1
2018-05-12 14:45:00 1.0 1
2018-05-12 15:00:00 1.0 1
我尝试了各种方法,使用辅助列,但没有一个产生任何接近我想要的东西。 任何帮助将非常感谢:)
您可以执行以下操作:
th = 3 # set threshold
# Sets to True rows that are 0
x = df.DSWRF_integ.eq(0)
# Takes the cumulative sum of rows where changes occur (thus where diff != 0)
g = x.astype(int).diff().fillna(0).ne(0).cumsum()
# Groups the original df with g and replaces 0 to 1 where the length of consecutive zeroes
# is smaller than the threshold
ix = x[x].groupby(g[x]).transform('size').lt(th) = 1
df.loc[ix[ix].index, 'DSWRF_integ'] = 1
例
我创建了这个示例数据框,以便更轻松地检查结果数据帧。 我还创建了一个最终的数据pd.Series
添加了所有中间pd.Series
,以便更好地理解所有步骤:
df = pd.DataFrame({'col1':[0,0,0,2,1,3,0,1,2,0,0,0,0,1]})
现在,设置例如阈值4,应该变为1
全零,除了第9
行到第12
行中的零:
result = df.copy()
th = 4
x = df.col1.eq(0)
g = x.astype(int).diff().fillna(0).ne(0).cumsum()
ix = x[x].groupby(g[x]).transform('size').lt(th)
result.loc[ix[ix].index, 'col1'] = 1
df.assign(x=x, g=g, ix=ix, result=result)
col1 x g ix result
0 0 True 0 True 1
1 0 True 0 True 1
2 0 True 0 True 1
3 2 False 1 NaN 2
4 1 False 1 NaN 1
5 3 False 1 NaN 3
6 0 True 2 True 1
7 1 False 3 NaN 1
8 2 False 3 NaN 2
9 0 True 4 False 0
10 0 True 4 False 0
11 0 True 4 False 0
12 0 True 4 False 0
13 1 False 5 NaN 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.