I
I
Ivan Melnikov2021-12-06 13:54:59
Python
Ivan Melnikov, 2021-12-06 13:54:59

How to make a calculated field in a DataFrame by condition?

For example, a calculated field The problem is that m can be equal to 0. It is necessary that when m = 0, then the value of the calculated field is equal to 0. How to do this?
df['new'] = df.n / df.m

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexeytur, 2021-12-07
@immelnikoff

df['new'] = df.n / df.m if df.m != 0 else 0

M
Mikhail Krostelev, 2021-12-06
@twistfire92

Describe a function that takes n and m and divides n by m. If m==0 returns 0.
And then just call this function with your arguments:
df['new'] = foo(df.n, df.m)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question