Python – How to change the value of a looping dataframe based on the previous data
Question:
**
I’m trying to perform a value substitution for a fairly large dataframe.
C = Dataframe in question. It has value for the time 00:00 and I want the same value to be repeated 24 times when there is, so that it is the same at all times.
I try to loop through the dataframe and assign it the previous data when the value is 0.0. Since the value that appears is the average of the day.**
C = q.merge(ss, how='right',left_index=True, right_index=True)
C = C.fillna(0)
for index, row in C['H04_PEDRO_MARIN'].iteritems():
if row == 0.0:
C.replace({'H04_PEDRO_MARIN':{0.0:'Valor Anterior'}),inplace = True)
else:
None
C:
29/07/11 21:00 0
29/07/11 22:00 0
29/07/11 23:00 0
30/07/11 00:00 27658,625
30/07/11 01:00 0
30/07/11 02:00 0
30/07/11 03:00 0
30/07/11 04:00 0
30/07/11 05:00 0
30/07/11 06:00 0
30/07/11 07:00 0
30/07/11 08:00 0
30/07/11 09:00 0
30/07/11 10:00 0
30/07/11 11:00 0
30/07/11 12:00 0
30/07/11 13:00 0
30/07/11 14:00 0
30/07/11 15:00 0
30/07/11 16:00 0
30/07/11 17:00 0
30/07/11 18:00 0
30/07/11 19:00 0
30/07/11 20:00 0
30/07/11 21:00 0
30/07/11 22:00 0
30/07/11 23:00 0
31/07/11 00:00 32617,125
31/07/11 01:00 0
31/07/11 02:00 0
31/07/11 03:00 0`
I would like to have a solution like this one:
C:
29/07/11 21:00 0
29/07/11 22:00 0
29/07/11 23:00 0
30/07/11 00:00 27658,625
30/07/11 01:00 27658,625
30/07/11 02:00 27658,625
30/07/11 03:00 27658,625
30/07/11 04:00 27658,625
30/07/11 05:00 27658,625
30/07/11 06:00 27658,625
30/07/11 07:00 27658,625
30/07/11 08:00 27658,625
30/07/11 09:00 27658,625
30/07/11 10:00 27658,625
30/07/11 11:00 27658,625
30/07/11 12:00 27658,625
30/07/11 13:00 27658,625
30/07/11 14:00 27658,625
30/07/11 15:00 27658,625
30/07/11 16:00 27658,625
30/07/11 17:00 27658,625
30/07/11 18:00 27658,625
30/07/11 19:00 27658,625
30/07/11 20:00 27658,625
30/07/11 21:00 27658,625
30/07/11 22:00 27658,625
30/07/11 23:00 27658,625
31/07/11 00:00 32617,125
31/07/11 01:00 32617,125
31/07/11 02:00 32617,125
…
Answers:
Check the column dtype. I think you have string and not numeric values (because the thousand separator ‘,’).
Replace 0 or (‘0’) by np.nan
then front fill values:
df['H04_PEDRO_MARIN2'] = df.replace('0', np.nan).ffill().fillna(0)
print(df)
# Output
H04_PEDRO_MARIN H04_PEDRO_MARIN2
29/07/11 21:00 0 0
29/07/11 22:00 0 0
29/07/11 23:00 0 0
30/07/11 00:00 27658,625 27658,625
30/07/11 01:00 0 27658,625
30/07/11 02:00 0 27658,625
30/07/11 03:00 0 27658,625
30/07/11 04:00 0 27658,625
30/07/11 05:00 0 27658,625
30/07/11 06:00 0 27658,625
30/07/11 07:00 0 27658,625
30/07/11 08:00 0 27658,625
30/07/11 09:00 0 27658,625
30/07/11 10:00 0 27658,625
30/07/11 11:00 0 27658,625
30/07/11 12:00 0 27658,625
30/07/11 13:00 0 27658,625
30/07/11 14:00 0 27658,625
30/07/11 15:00 0 27658,625
30/07/11 16:00 0 27658,625
30/07/11 17:00 0 27658,625
30/07/11 18:00 0 27658,625
30/07/11 19:00 0 27658,625
30/07/11 20:00 0 27658,625
30/07/11 21:00 0 27658,625
30/07/11 22:00 0 27658,625
30/07/11 23:00 0 27658,625
31/07/11 00:00 32617,125 32617,125
31/07/11 01:00 0 32617,125
31/07/11 02:00 0 32617,125
31/07/11 03:00 0 32617,125
**
I’m trying to perform a value substitution for a fairly large dataframe.
C = Dataframe in question. It has value for the time 00:00 and I want the same value to be repeated 24 times when there is, so that it is the same at all times.
I try to loop through the dataframe and assign it the previous data when the value is 0.0. Since the value that appears is the average of the day.**
C = q.merge(ss, how='right',left_index=True, right_index=True)
C = C.fillna(0)
for index, row in C['H04_PEDRO_MARIN'].iteritems():
if row == 0.0:
C.replace({'H04_PEDRO_MARIN':{0.0:'Valor Anterior'}),inplace = True)
else:
None
C:
29/07/11 21:00 0
29/07/11 22:00 0
29/07/11 23:00 0
30/07/11 00:00 27658,625
30/07/11 01:00 0
30/07/11 02:00 0
30/07/11 03:00 0
30/07/11 04:00 0
30/07/11 05:00 0
30/07/11 06:00 0
30/07/11 07:00 0
30/07/11 08:00 0
30/07/11 09:00 0
30/07/11 10:00 0
30/07/11 11:00 0
30/07/11 12:00 0
30/07/11 13:00 0
30/07/11 14:00 0
30/07/11 15:00 0
30/07/11 16:00 0
30/07/11 17:00 0
30/07/11 18:00 0
30/07/11 19:00 0
30/07/11 20:00 0
30/07/11 21:00 0
30/07/11 22:00 0
30/07/11 23:00 0
31/07/11 00:00 32617,125
31/07/11 01:00 0
31/07/11 02:00 0
31/07/11 03:00 0`
I would like to have a solution like this one:
C:
29/07/11 21:00 0
29/07/11 22:00 0
29/07/11 23:00 0
30/07/11 00:00 27658,625
30/07/11 01:00 27658,625
30/07/11 02:00 27658,625
30/07/11 03:00 27658,625
30/07/11 04:00 27658,625
30/07/11 05:00 27658,625
30/07/11 06:00 27658,625
30/07/11 07:00 27658,625
30/07/11 08:00 27658,625
30/07/11 09:00 27658,625
30/07/11 10:00 27658,625
30/07/11 11:00 27658,625
30/07/11 12:00 27658,625
30/07/11 13:00 27658,625
30/07/11 14:00 27658,625
30/07/11 15:00 27658,625
30/07/11 16:00 27658,625
30/07/11 17:00 27658,625
30/07/11 18:00 27658,625
30/07/11 19:00 27658,625
30/07/11 20:00 27658,625
30/07/11 21:00 27658,625
30/07/11 22:00 27658,625
30/07/11 23:00 27658,625
31/07/11 00:00 32617,125
31/07/11 01:00 32617,125
31/07/11 02:00 32617,125
…
Check the column dtype. I think you have string and not numeric values (because the thousand separator ‘,’).
Replace 0 or (‘0’) by np.nan
then front fill values:
df['H04_PEDRO_MARIN2'] = df.replace('0', np.nan).ffill().fillna(0)
print(df)
# Output
H04_PEDRO_MARIN H04_PEDRO_MARIN2
29/07/11 21:00 0 0
29/07/11 22:00 0 0
29/07/11 23:00 0 0
30/07/11 00:00 27658,625 27658,625
30/07/11 01:00 0 27658,625
30/07/11 02:00 0 27658,625
30/07/11 03:00 0 27658,625
30/07/11 04:00 0 27658,625
30/07/11 05:00 0 27658,625
30/07/11 06:00 0 27658,625
30/07/11 07:00 0 27658,625
30/07/11 08:00 0 27658,625
30/07/11 09:00 0 27658,625
30/07/11 10:00 0 27658,625
30/07/11 11:00 0 27658,625
30/07/11 12:00 0 27658,625
30/07/11 13:00 0 27658,625
30/07/11 14:00 0 27658,625
30/07/11 15:00 0 27658,625
30/07/11 16:00 0 27658,625
30/07/11 17:00 0 27658,625
30/07/11 18:00 0 27658,625
30/07/11 19:00 0 27658,625
30/07/11 20:00 0 27658,625
30/07/11 21:00 0 27658,625
30/07/11 22:00 0 27658,625
30/07/11 23:00 0 27658,625
31/07/11 00:00 32617,125 32617,125
31/07/11 01:00 0 32617,125
31/07/11 02:00 0 32617,125
31/07/11 03:00 0 32617,125