Reputation: 48
So I have a dataframe of daily weather data and if the temp is above 0. e.g:
|date|temp|pos_temp|
I am trying to make a column that has a cumulative sum for days in a row with temperatures above zero. I made a one-hot encoded column with a 1 or 0 for days above 0, but I'm having trouble counting the cumulative days with a '1' in the 'positive' column.
example
date |temp|pos_temp|
2020-04-27|1 |1 |
2020-04-28|-1 |0 |
2020-04-29|-2 |0 |
2020-04-30|4 |1 |
2020-05-01|7 |1 |
2020-05-02|10 |1 |
2020-05-03|14 |1 |
2020-05-04|13 |1 |
I'm trying to make a column like this:
date |temp|pos_temp|cum_above_0
2020-04-27|1 |1 |nan
2020-04-28|-1 |0 |0
2020-04-29|-2 |0 |0
2020-04-30|4 |1 |1
2020-05-01|7 |1 |2
2020-05-02|10 |1 |3
2020-05-03|14 |1 |4
2020-05-04|13 |1 |5
Upvotes: 3
Views: 766
Reputation: 86
you can iterate on every line of your data frame.
temp = pd.DataFrame({'temp':[1, -1, -2, 4, 7, 10, 14, 13]})
count = 0
for index, row in temp.iterrows():
if row['temp'] > 0:
count += 1
else:
count = 0
temp.loc[index, 'cum_above_0'] = count
temp cum_above_0
0 1 1.0
1 -1 0.0
2 -2 0.0
3 4 1.0
4 7 2.0
5 10 3.0
6 14 4.0
7 13 5.0
Upvotes: 3