I want to replace negative values in a pandas DataFrame column with zero.
Is there a more concise way to construct this expression?
df['value'][df['value'] < 0] = 0
You could use the clip method:
import pandas as pd import numpy as np df = pd.DataFrame({'value': np.arange(-5,5)}) df['value'] = df['value'].clip(0, None) print(df)
yields
value 0 0 1 0 2 0 3 0 4 0 5 0 6 1 7 2 8 3 9 4
1.4m articles
1.4m replys
5 comments
57.0k users