1

I have a data frame as follows:

enter image description here

Now I want to divide the x by y values without giving me an exception. For example when I divide 3 by 2 it should give me 1.5 and when I divide 3 by 0 it should give me zero. To achieve this I have written an exception function

def divide(x,y):
    try:
        result = x/y
        print (result)
    except ZeroDivisionError:
        print (0)

I want to now create a new column in the data-frame and apply this function. So far I have used:

df['num']  = df.apply(divide)

But this is not giving me the required result. Can someone help

1
  • use return x/y instead of print Commented Apr 17, 2019 at 6:32

3 Answers 3

2

You can also try:

def divide(df):
    if(df['y']!=0):
        return df['x']/df['y']
    else:
        return 0
df['num']=df.apply(divide,axis=1)

But I would suggest using this instead:

import numpy as np
df['num']=df['x']/df['y']
df.replace(np.inf,0)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

if you are using python 2.7 then return x/float(y)

import math

def divide(x,y):
        # if you check type(y) or type(x) you will get type: numpy.float64
        # pandas internally converts it to a numpy.float64
        # when you do division on such values you will get inf as output
        # you can check if values are not zero and do calculations or convert it to float like: x / float(y)

        if y and not math.isnan(y):
            return x/y

df['num']  = df.apply(lambda row: divide(row["x"], row["y"]), axis=1)

7 Comments

I'm using python 3.7
I ran the code but it's giving me inf values when a number is being divided by 0. Perhaps the divide function is not working properly
x and y are both present in my df. the formula works when dividing numbers like 17 by 3 but then it divides 11 by 0 it gives me inf values. To counter this we wrote the exception formula. I wonder why that is not working
inf is not a ZeroDivisionError. it's just giving an answer as infinite., Check data type of x and y. Is it numpy.float64 or silimar rather than float/int?
|
0

You need to return the stuff, not print the stuff, so it doesn't just displays, but also stores in memory:

def divide(x,y):
    try:
        return x/y
    except ZeroDivisionError:
        return 0

df['num'] = df.apply(lambda x: divide(x["x"], x["y"]), axis=1)

3 Comments

Okay. Thanks for pointing out my mistake. Now if I want to create a new column and apply it on my df it is still giving me an error. I'm using : df['num'] = df.apply(divide)
@OmerQureshi edited my answer, please accept and up-vote it if it works.
I ran the code but it is giving me inf for numbers which are dividing by 0. This is exactly what I wanted to avoid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.