Python
All Top Python Interview Questions#27
Handle Infinite Values and Division by Zero in Pandas
EasyInterview Question #27
When dividing revenue by ad spend where spend is 0, Pandas produces `inf`. Detect all infinite values in the DataFrame and replace them with `NaN`.
Input Table: df
2 rows preview| revenue | ad_spend |
|---|---|
| 1000 | 0 |
| 5000 | 1000 |
Expected Output Structure2 rows
| roas |
|---|
| NULL |
| 5 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Starting the real Python + pandas runtime (one-time download)…