0

I have a dataframe with multiple columns and trying to find out local minima and maxima for only one of the column data. For which, I am using below code

df['min'] = df.histo[(df.histo.shift(1) > df.histo) & (df.histo.shift(-1) > df.histo)]
df['max'] = df.histo[(df.histo.shift(1) < df.histo) & (df.histo.shift(-1) < df.histo)]

It does not add those two outputs into the same dataframe and throws the error mentioned in the question. However, if I want to save this into a new dataframe same code works, but that I don't want. I understand when updating same dataframe for local minima and maxima it should put "NaN" at all other locations but somehow the command is not working. Also i found a similar thread which shows the example (https://stackoverflow.com/a/48024165/13079994) for some random dataframe. Please help!

  • 1
    That example only works, if the resulting series index can be "paired" with the original index of the df. If your index is duplicated, it can not do the assignment. So the answer is, that you need to deduplicate your index. Or if you insist on using duplicated index, you can use something like: `df['min'] = df.histo.mask(~((df.histo.shift(1) > df.histo) & (df.histo.shift(-1) > df.histo)))` – Betelgeux May 02 '21 at 19:30
  • Thanks, it's working now. – user13079994 May 02 '21 at 19:42

0 Answers0