replace negative values with 0

PHOTO EMBED

Thu Jan 20 2022 08:53:52 GMT+0000 (Coordinated Universal Time)

Saved by @CaoimhedeFrein #python

df = pd.DataFrame({'A': [-3, 7, 4, 0], 'B': [-6, -1, 2, -8], 'C': [1, 2, 3, 4]})

#it goes through column A, selects where it's negative & replaces with 2, or if it's not negative it puts in the values from column C
df.A = np.where(df.A < 0, 2, df.C)


#it goes through column A, selects where it's negative & replaces with 2, or if it's not negative it leaves it as is
df.A = np.where(df.A < 0, 2, df.A)
content_copyCOPY