Reputation: 51
I have a pandas dataframe that contains river levels and rainfalls together in a DataFrame called Hourly. I would like to be able to loop through for every rainfall value and collect its river level value, plus the next river level reading. For Example:
River Level Rainfall
0.876 0.0
0.877 0.8
0.882 0.0
In this case if I was looking for the values for 0.8mm of rainfall, I would like it to return the 0.877 that is in the same row as the 0.8 and also the 0.882 in the row immediately after. I would like it to output:
0.877
0.882
Currently I have a loop that goes through and locates all the rows for a given rainfall value but cannot figure out how to get the one row after value.
Any help greatly appreciated.
Upvotes: 1
Views: 67
Reputation: 514
shift is the way to go as suggested by @Andy L. If you are looking for alternatives here's another way (after Wayne suggestion):
rainfall_value = 0.8
index = data[data.Rainfall == rainfall_value].index.tolist()
index = [item for x in index for item in [x, x+1]]
result = data.iloc[index]
print(result['River Level'])
# 1 0.877
# 2 0.882
Upvotes: 1
Reputation: 25269
Try this
s = df.Rainfall.eq(0.8)
out = df.loc[s | s.shift(), 'River Level']
Out[364]:
1 0.877
2 0.882
Name: River Level, dtype: float64
Upvotes: 2