I have a Python Dataframe with a unique column df['A'] containing random values, and the DF index are dates not unique. I want to do:
If A>0 : df.A = df.A +1
If A<0 : df.A = df.A -1
So I tried
df.loc[df.A>0, 'A'] = df.A +1
df.loc[df.A<0, 'A'] = df.A -1
Output:
pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects
I accept the error as it's true that I don't have a unique index. Question #1: why do I need a unique index to do such as simple operation that does not seem to involve the index?
Now I found a workaround, it's not good looking but it works. Question #2: What do you think of this workaround?
df.loc[df.A>0, 'A'] = df.loc[df.A>0, 'A'] +1
df.loc[df.A<0, 'A'] = df.loc[df.A<0, 'A'] -1
Question #3: are there any other way to do that that I haven't explored? I thought about using .apply(lambda x: but I'm getting the same error as above about the index pandas.errors.InvalidIndexError:.
Thanks all, have a lovely day!
