PY DF : add +1 to a field based on a condition on the same field (with non unique index)

Viewed 27

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!

1 Answers

The dataframe I took for reference is this:

enter image description here

Answer 1:

I'm not sure but maybe it causes an issue in the case of duplicate indexes because of the dilemma that which value should be changed, since both of them are going to be referenced by the same name. for example: if duplicate index is '2021-12-31', then they're going to be referenced by df.loc['2021-12-31'].Again, I'm not sure about this answer..

Answer 2:

The code that you have written does seem to work but it may take up a lot of time when there is a huge dataset with a lot of values, since you're filtering and computing it twice.

Answer 3:

I've tried to achieve the same using lambda function and it worked perfectly fine for me. The code I used:

def new_value(x):
    if(x>0):
        return x+1
    else:
        return x-1

df['A'] = df['A'].apply(lambda x: new_value(x))
Related