Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the min of [0, x] element wise for a column

Tags:

python

pandas

I need to compute a column where the value is the result of a vectorized operation over other columns:

df["new_col"] = df["col1"] - min(0,df["col2"])

It turned out, however, that I cannot use min as in the above syntax. So, what is the right way to get the min between zero and a given value of pandas column?

like image 677
Mohamed Ali JAMAOUI Avatar asked Aug 14 '15 11:08

Mohamed Ali JAMAOUI


People also ask

How do you find the min of a column in a Dataframe?

Use min() function on a dataframe with 'axis = 1' attribute to find the minimum value over the row axis. 3) Get minimum values of every column without skipping None Value : Use min() function on a dataframe which has Na value with 'skipna = False' attribute to find the minimum value over the column axis.

How do I get the minimum of two columns in pandas?

Min value between two pandas columns You can do so by using the pandas min() function twice.

How do you find the max and min of a column in pandas?

Pandas dataframe. max() method finds the maximum of the values in the object and returns it. If the input is a series, the method will return a scalar which will be the maximum of the values in the series.

How do you find the minimum value in a series in Python?

min() function to find the minimum value of the given series object. Output : As we can see in the output, the Series. min() function has successfully returned the minimum value of the given series object.


2 Answers

you can use numpy.minimum to find the element-wise minimum of an array

import numpy as np
df["new_col"] = df["col1"] - np.minimum(0,df["col2"])
like image 109
tmdavison Avatar answered Oct 17 '22 15:10

tmdavison


You could use some masking and a temporary column. Totally ignoring the 'min' function.

magicnumber = 0
tempcol = df['col2']
mask = tempcol < magicnumber
tempcol.loc[df[~mask].index] = magicnumber
df['col1'] - tempcol

Or you can use a lambda function:

magicnumber = 0
df['col1'] - df['col2'].apply(lambda x: np.min(magicnumber, x))

OR you can apply over two columns:

df['magicnumber'] = 0
df['col1'] - df[['col2', 'magicnumber']].apply(np.min, axis=1)
like image 1
firelynx Avatar answered Oct 17 '22 16:10

firelynx