Home > other >  How to change values of masked column in pandas?
How to change values of masked column in pandas?

Time:10-22

How can I change the values of a masked column of a pandas dataframe? The goal is to copy the exchange the values by the values of another column. (I want to implement these lines of code later on in a loop, thats why I use "i")

i = 0
mask = (df.index > df_dates.start[i]) & (df.index < df_dates.end[i])
df.loc[mask].events = df.loc[mask].l_mean
df.loc[mask].events

I get no error message, however the values are not changing:

TIMESTAMP
2020-05-17 22:32:00    0.0
2020-05-17 22:33:00    0.0
2020-05-17 22:34:00    0.0
2020-05-17 22:35:00    0.0
2020-05-17 22:36:00    0.0
                      ... 
2020-05-18 03:50:00    0.0
2020-05-18 03:51:00    0.0
2020-05-18 03:52:00    0.0
2020-05-18 03:53:00    0.0
2020-05-18 03:54:00    0.0

CodePudding user response:

Do not chain the assign function pass it to loc

df.loc[mask, 'events'] = df.loc[mask, 'l_mean']
  • Related