Python
All Top 50 Python & Pandas 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: roas_table
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
Click "Run & Test" to execute your Pandas code against the test assertions.