# Python: Divide two columns, a/b, if b is 0 -> assign 0

## Question:

I need to make new column in df (apples per pupils) divide two columns (apples and pupils), and if the number of pupils is 0, I should just assign zero to that row.

Current values:

apples | pupils |
---|---|

5 | 2 |

6 | 3 |

5 | 0 |

Expected output:

apples | pupils | avg_apples_per_pupils |
---|---|---|

5 | 2 | 2.5 |

6 | 3 | 2.0 |

5 | 0 | 0.0 |

I tried using this:

```
if df[df['pupils']!=0]:
df['avg_apples_per_pupils'] = df['apples'] / df['pupils']
else:
df['avg_apples_per_pupils'] = df.assign(avg_apples_per_pupils='0')
```

but I get this error:

```
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
```

I’m completely open for other approaches

## Answers:

Because you compere array and 0 (if df[df[‘pupils’]!=0)

you must specify which value from the df[‘pupils’] you want to compere to 0

```
if(df['pupils'] != 0).any():
df['avg_apples_per_pupils']=df['apples'] / df['pupils']
```

else:

df[‘avg_apples_per_pupils’] = 0

Use `np.where`

condition:

```
df['avg_apples_per_pupils'] = np.where(df['pupils'].ne(0), df['apples'] / df['pupils'], df['pupils'])
```

```
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
```

You can use a `mask`

:

```
df['avg_apples_per_pupils'] = (df['apples'].div(df['pupils'])
.mask(df['pupils'].eq(0), 0)
)
```

Or `numpy.divide`

:

```
df['avg_apples_per_pupils'] = np.divide(df['apples'], df['pupils'],
where=df['pupils'].ne(0))
```

Output:

```
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
```