Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return max of zero or value for a pandas DataFrame column

Tags:

python

pandas

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 
like image 443
bjornarneson Avatar asked Jun 12 '13 14:06

bjornarneson


People also ask

How do you find the maximum value of A Pandas DataFrame column in Python?

Find Maximum Element in Pandas DataFrame's Row Finding the max element of each DataFrame row relies on the max() method as well, but we set the axis argument to 1 . The default value for the axis argument is 0. If the axis equals to 0, the max() method will find the max element of each column.

What does LOC in Pandas return?

The loc property is used to access a group of rows and columns by label(s) or a boolean array. . loc[] is primarily label based, but may also be used with a boolean array.


1 Answers

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 
like image 188
unutbu Avatar answered Sep 24 '22 15:09

unutbu