Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas - dividing column by another column

I'm trying to add a column to my DataFrame which is the product of division of two other columns, like so:

df['$/hour'] = df['$']/df['hours'] 

This works fine, but if the value in ['hours'] is less than 1, then the ['$/hour'] value is greater than the value in ['$'], which is not what I want.

Is there a way of controlling the operation so that if ['hours'] < 1 then df['$/hour'] = df['$']?

like image 760
Charon Avatar asked Feb 16 '16 17:02

Charon


People also ask

Can you divide one column by another in pandas?

The simple division (/) operator is the first way to divide two columns. You will split the First Column with the other columns here. This is the simplest method of dividing two columns in Pandas.

How do you divide a column by another in Python?

The second method to divide two columns is using the div() method. It divides the columns elementwise. It accepts a scalar value, series, or dataframe as an argument for dividing with the axis. If the axis is 0 the division is done row-wise and if the axis is 1 then division is done column-wise.

How do you do division in pandas?

Pandas DataFrame: div() functionThe div() function returns floating division of dataframe and other, element-wise (binary operator truediv). Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **. Any single or multiple element data structure, or list-like object.

How do I divide one data frame by another?

div() method divides element-wise division of one pandas DataFrame by another. DataFrame elements can be divided by a pandas series or by a Python sequence as well. Calling div() on a DataFrame instance is equivalent to invoking the division operator (/).


1 Answers

You can use numpy.where:

print df     hours  $ 0       0  8 1       0  9 2       0  9 3       3  6 4       6  4 5       3  7 6       5  5 7      10  1 8       9  3 9       3  6 10      5  4 11      5  7  df['$/hour'] = np.where(df['hours'] < 1, df['hours'], df['$']/df['hours']) print df     hours  $    $/hour 0       0  8  0.000000 1       0  9  0.000000 2       0  9  0.000000 3       3  6  2.000000 4       6  4  0.666667 5       3  7  2.333333 6       5  5  1.000000 7      10  1  0.100000 8       9  3  0.333333 9       3  6  2.000000 10      5  4  0.800000 11      5  7  1.400000 
like image 69
jezrael Avatar answered Sep 24 '22 15:09

jezrael