Skip to content
Advertisement

Pandas – Reverse sign of column value based on another value

Can anyone suggest how to do this currectly.

Currently I am using apply but it obviously goes through each cell and takes ages.

list-price – Float ordtype = str (“I”,”C”,”K”)

I want all the “C” & “K” values in the column ordtype to be negative (they are currently positive)

example code i’m using now:

final_lines["list-price"] = final_lines.apply(lambda x: x["list-price"]*-1 if x["ordtype"] != "I" else x["list-price"])

Advertisement

Answer

Try with:

df.loc[df['ordtype'].ne('I'), 'list-price'] *= -1
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement