Reputation: 1189
I want to indicate whether the temperature dropped below 15°C in a data frame. How to do it more efficiently?
df['Was cold'] = df['Temperature']<15
df['Jacket needed'] = False
for i in range(len(df)-8):
df.iloc[i].loc['Jacked needed'] = df.iloc[i:(i+8)]['Was cold'].any()
The data are ordered by time and each record represents an hour. df['Was cold']
indicates whether the temperature was less than 15°C. df['Jacket needed']
means that in the next 8 hours there was at least one when it was cold and I would need a jacket if I would have to go out.
Upvotes: 0
Views: 36
Reputation: 3462
Would the following work?
df['Jacket needed'] = df['Was cold'].rolling(window=8).sum().shift(-7) > 0
Upvotes: 1