I want to change a value from a row/column of a dataframe as follows:
Within the interval, when it found a value which is different equal to 1, then it should put 2.
The example:
initial df:
index event
2019-12-07 18:30:16 0
2019-12-07 19:30:16 0
2019-12-07 20:30:16 0
2019-12-07 21:30:16 0
2019-12-07 22:30:16 1
wanted df:
index event
2019-12-07 18:30:16 0
2019-12-07 19:30:16 0
2019-12-07 20:30:16 0
2019-12-07 21:30:16 0
2019-12-07 22:30:16 2
The following code works but I cannot change the value:
mask = (df.index > start_dates) & (df.index <= end_dates)
for k in range (0, len(df.loc[mask])):
if df.loc[mask].event[k] == 1:
df.loc[mask].loc[df.loc[mask].event == 1, "event"] = 2
I cannot change the value from 1 to 2 in the last line of code.
I also tried this...:
df.loc[mask].loc[df.loc[mask].event == 1, "event"] = 2
df.loc[mask].event[df.loc[mask].event == '1'] = 2
df.loc[mask].event[k] = 2
But none of the above lines works.
Please help me. :( Any help is highly appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…