1

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
eetiaro
  • 342
  • 1
  • 3
  • 14
  • 1
    Chained assignments is the issue. Use -> `df.loc[mask, 'events']= df.loc[mask, 'l_mean']`. [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/a/20627316/15497888) – Henry Ecker Oct 21 '21 at 13:47

1 Answers1

1

Do not chain the assign function pass it to loc

df.loc[mask, 'events'] = df.loc[mask, 'l_mean']
BENY
  • 317,841
  • 20
  • 164
  • 234