#Handle infinity values in Python
In some cases where a float is divided by zero, you may encounter infinity values, negative or positive: np.inf or -np.inf.
# Import libraries
import pandas as pd
import numpy as np
from numpy.random import randint
# Create sample dataframe
df = (
pd.DataFrame({'clicks': randint(1, 20, 5),
'impressions': list(randint(1000, 3000, 4)) + [0]})
.assign(ctr=lambda x: x['clicks']/x['impressions'])
)
df| clicks | impressions | ctr | |
|---|---|---|---|
| 0 | 7 | 2850 | 0.002456 |
| 1 | 11 | 1896 | 0.005802 |
| 2 | 9 | 1457 | 0.006177 |
| 3 | 9 | 1698 | 0.005300 |
| 4 | 4 | 0 | inf |
Replace infinity values
To replace those infinity values, you can use replace():
# Replace infinity with 0
df.replace([np.inf, -np.inf], 0)| clicks | impressions | ctr | |
|---|---|---|---|
| 0 | 7 | 2850 | 0.002456 |
| 1 | 11 | 1896 | 0.005802 |
| 2 | 9 | 1457 | 0.006177 |
| 3 | 9 | 1698 | 0.005300 |
| 4 | 4 | 0 | 0.000000 |
Drop rows
If you want to drop the rows, replace infinity values with NaN and follow with dropna():
# Replace with NaN and drop rows
df.replace([np.inf, -np.inf], np.nan).dropna()| clicks | impressions | ctr | |
|---|---|---|---|
| 0 | 7 | 2850 | 0.002456 |
| 1 | 11 | 1896 | 0.005802 |
| 2 | 9 | 1457 | 0.006177 |
| 3 | 9 | 1698 | 0.005300 |