ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://saturncloud.io/blog/python-pandas-how-to-remove-nan-and-inf-values/ |
| Last Crawled | 2026-04-07 07:06:11 (1 day ago) |
| First Indexed | 2023-11-02 16:46:09 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Pandas: How to remove nan and inf values | Saturn Cloud Blog |
| Meta Description | In this blog, learn how to effectively handle missing and invalid data using Python pandas for seamless data analysis. |
| Meta Canonical | null |
| Boilerpipe Text | As a data scientist or software engineer, you know that working with data can be challenging, especially when dealing with missing or invalid values. In this post, I’ll show you how to use
Python
pandas
to remove
NaN
and
-inf
values from your data.
What are
NaN
and
-inf
values?
NaN
stands for
Not a Number
and is a special floating-point value used to represent missing or undefined values.
NaN
values can occur when performing mathematical operations on invalid values, such as dividing by zero or taking the square root of a negative number.
-Inf
stands for negative infinity and is another special floating-point value used to represent values that are too small to be represented by a finite number.
-Inf
values can occur when performing mathematical operations on extremely small values.
NaN
and
-inf
values can cause problems when performing calculations or statistical analysis on your data. They can skew your results, produce incorrect values, or cause errors in your code. Therefore, it’s often necessary to remove them before proceeding with your analysis.
How to remove
NaN
and
-inf
values in Python pandas
Python pandas provides several methods for removing
NaN
and
-inf
values from your data. The most commonly used methods are:
dropna()
: removes rows or columns with
NaN
or
-inf
values
replace()
: replaces
NaN
and
-inf
values with a specified value
interpolate()
: fills
NaN
values with interpolated values
Using
dropna()
The
dropna()
method removes rows or columns with
NaN
or
-inf
values from your data. By default, it removes all rows with at least one
NaN
or
-inf
value. You can specify the axis parameter to remove columns instead of rows.
import
pandas
as
pd
# create a dataframe that contains NaN values
df
=
pd
.
DataFrame
({
'A'
:
[
1
,
2
,
3
,
4
,
5
],
'B'
:
[
6
,
-
7
,
8
,
-
9
,
10
],
'C'
:
[
11
,
12
,
13
,
None
,
15
],
'D'
:
[
16
,
17
,
18
,
19
,
20
]
})
print
(
df
)
Output:
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 NaN 19
4 5 10 15.0 20
# drop rows that contain NaN values
df
=
df
.
dropna
()
print
(
df
)
Output:
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
4 5 10 15.0 20
In this example, the
dropna()
method removes the fourth row from the DataFrame, which contains a None value in column C.
Using
replace()
The
replace()
method replaces
NaN
and
-inf
values with a specified value. You can specify the value to replace
NaN
and
-inf
with using the
value
parameter.
import
pandas
as
pd
import
numpy
as
np
# create a dataframe that contains NaN and -inf values
df
=
pd
.
DataFrame
({
'A'
:
[
1
,
2
,
3
,
4
,
5
],
'B'
:
[
6
,
-
7
,
8
,
-
9
,
10
],
'C'
:
[
11
,
12
,
13
,
np
.
nan
,
15
],
'D'
:
[
16
,
17
,
-
np
.
inf
,
19
,
20
]
})
print
(
df
)
Output:
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 -inf
3 4 -9 NaN 19.0
4 5 10 15.0 20.0
# replace NaN and -inf values with 0
df
=
df
.
replace
([
np
.
nan
,
-
np
.
inf
],
0
)
print
(
df
)
Output:
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 0.0
3 4 -9 0.0 19.0
4 5 10 15.0 20.0
In this example, the
replace()
method replaces all
NaN
and
-inf
values with 0.
Using
interpolate()
The
interpolate()
method fills
NaN
values with interpolated values based on the values of neighboring rows or columns. You can specify the interpolation method to use using the
method
parameter.
import
pandas
as
pd
import
numpy
as
np
df
=
pd
.
DataFrame
({
'A'
:
[
1
,
2
,
3
,
4
,
5
],
'B'
:
[
6
,
-
7
,
8
,
-
9
,
10
],
'C'
:
[
11
,
12
,
13
,
np
.
nan
,
15
],
'D'
:
[
16
,
17
,
18
,
19
,
20
]
})
# Interpolate a value to replace NaN based on its neighbors
df
=
df
.
interpolate
(
method
=
'linear'
)
Output:
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 14.0 19
4 5 10 15.0 20
In this example, the
interpolate()
method fills the
NaN
value in column C with an interpolated value
(14)
based on the values of neighboring rows.
Conclusion
NaN
and
-inf
values can cause problems when working with data, but Python pandas provides several methods for removing or replacing them. By using the
dropna()
,
replace()
, and
interpolate()
methods, you can clean your data and proceed with your analysis without worrying about invalid values.
Remember to always carefully consider the impact of removing or replacing
NaN
and
-inf
values on your analysis and to document your data cleaning process. |
| Markdown | [📣 **Introducing \$2.95/Hr H100, H200, B200s, and B300s**: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure 📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem.](https://saturncloud.io/h100) [📣 **Introducing \$2.95/Hr H100, H200, B200s, and B300s**: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure 📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem.](https://saturncloud.io/h100) [📣 **Introducing \$2.95/Hr H100, H200, B200s, and B300s**: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure 📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem.](https://saturncloud.io/h100)
[](https://saturncloud.io/)
- [Partners](https://saturncloud.io/blog/python-pandas-how-to-remove-nan-and-inf-values/)
- [Mirantis](https://saturncloud.io/partners/mirantis/)
- [Nebius](https://saturncloud.io/partners/nebius/)
- [Crusoe](https://saturncloud.io/partners/crusoe/)
- [SUSE](https://saturncloud.io/partners/suse/)
- [NVIDIA](https://saturncloud.io/partners/nvidia/)
- [AWS](https://saturncloud.io/partners/aws/)
- [Snowflake](https://saturncloud.io/partners/snowflake/)
- [Azure](https://saturncloud.io/partners/azure/)
- [Resources](https://saturncloud.io/blog/python-pandas-how-to-remove-nan-and-inf-values/)
- [Quickstart](https://saturncloud.io/docs/quickstart/)
- [API](https://api.saturncloud.io/)
- [Blog](https://saturncloud.io/blog/)
- [Get Help](https://saturncloud.io/help/)
- [Docs](https://saturncloud.io/docs/)
- [Plans & Pricing](https://saturncloud.io/plans/saturn_cloud_plans/)
- [Enterprise](https://saturncloud.io/plans/enterprise/)
- [Login](https://app.community.saturnenterprise.io/auth/login)
[Start for free](https://app.community.saturnenterprise.io/auth/hosted-registration)
[← Back to Blog](https://saturncloud.io/blog/)
Miscellaneous
# Python Pandas: How to remove nan and inf values
In this blog, learn how to effectively handle missing and invalid data using Python pandas for seamless data analysis.
Saturn Cloud
June 19, 2023
Updated
October 21, 2023

As a data scientist or software engineer, you know that working with data can be challenging, especially when dealing with missing or invalid values. In this post, I’ll show you how to use [Python](https://saturncloud.io/glossary/python) [pandas](https://saturncloud.io/glossary/pandas) to remove `NaN` and `-inf` values from your data.
## What are `NaN` and `-inf` values?
`NaN` stands for *Not a Number* and is a special floating-point value used to represent missing or undefined values. `NaN` values can occur when performing mathematical operations on invalid values, such as dividing by zero or taking the square root of a negative number.
`-Inf` stands for negative infinity and is another special floating-point value used to represent values that are too small to be represented by a finite number. `-Inf` values can occur when performing mathematical operations on extremely small values.
## Why remove `NaN` and `-inf` values?
`NaN` and `-inf` values can cause problems when performing calculations or statistical analysis on your data. They can skew your results, produce incorrect values, or cause errors in your code. Therefore, it’s often necessary to remove them before proceeding with your analysis.
## How to remove `NaN` and `-inf` values in Python pandas
Python pandas provides several methods for removing `NaN` and `-inf` values from your data. The most commonly used methods are:
- `dropna()`: removes rows or columns with `NaN` or `-inf` values
- `replace()`: replaces `NaN` and `-inf` values with a specified value
- `interpolate()`: fills `NaN` values with interpolated values
### Using `dropna()`
The `dropna()` method removes rows or columns with `NaN` or `-inf` values from your data. By default, it removes all rows with at least one `NaN` or `-inf` value. You can specify the axis parameter to remove columns instead of rows.
```
import pandas as pd
# create a dataframe that contains NaN values
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, None, 15],
'D': [16, 17, 18, 19, 20]
})
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 NaN 19
4 5 10 15.0 20
```
```
# drop rows that contain NaN values
df = df.dropna()
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
4 5 10 15.0 20
```
In this example, the `dropna()` method removes the fourth row from the DataFrame, which contains a None value in column C.
### Using `replace()`
The `replace()` method replaces `NaN` and `-inf` values with a specified value. You can specify the value to replace `NaN` and `-inf` with using the `value` parameter.
```
import pandas as pd
import numpy as np
# create a dataframe that contains NaN and -inf values
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, np.nan, 15],
'D': [16, 17, -np.inf, 19, 20]
})
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 -inf
3 4 -9 NaN 19.0
4 5 10 15.0 20.0
```
```
# replace NaN and -inf values with 0
df = df.replace([np.nan, -np.inf], 0)
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 0.0
3 4 -9 0.0 19.0
4 5 10 15.0 20.0
```
In this example, the `replace()` method replaces all `NaN` and `-inf` values with 0.
### Using `interpolate()`
The `interpolate()` method fills `NaN` values with interpolated values based on the values of neighboring rows or columns. You can specify the interpolation method to use using the `method` parameter.
```
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, np.nan, 15],
'D': [16, 17, 18, 19, 20]
})
# Interpolate a value to replace NaN based on its neighbors
df = df.interpolate(method='linear')
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 14.0 19
4 5 10 15.0 20
```
In this example, the `interpolate()` method fills the `NaN` value in column C with an interpolated value `(14)` based on the values of neighboring rows.
## Conclusion
`NaN` and `-inf` values can cause problems when working with data, but Python pandas provides several methods for removing or replacing them. By using the `dropna()`, `replace()`, and `interpolate()` methods, you can clean your data and proceed with your analysis without worrying about invalid values.
Remember to always carefully consider the impact of removing or replacing `NaN` and `-inf` values on your analysis and to document your data cleaning process.
### About Saturn Cloud
Saturn Cloud is a portable AI platform that installs securely in any cloud account. Build, deploy, scale and collaborate on AI/ML workloads.
[Start for free](https://saturncloud.io/blog/python-pandas-how-to-remove-nan-and-inf-values/#request-a-demo)
Keep reading
## Related articles
[Miscellaneous Dec 29, 2023 How to Resolve Memory Errors in Amazon SageMaker](https://saturncloud.io/blog/how-to-resolve-memory-errors-in-amazon-sagemaker/)
[Miscellaneous Dec 22, 2023 Loading S3 Data into Your AWS SageMaker Notebook: A Guide](https://saturncloud.io/blog/loading-s3-data-into-your-aws-sagemaker-notebook-a-comprehensive-guide/)
[Miscellaneous Dec 19, 2023 How to Convert Pandas Series to DateTime in a DataFrame](https://saturncloud.io/blog/how-to-convert-pandas-series-to-datetime-in-a-dataframe/)

Saturn Cloud, Inc
228 Park Ave S, PMB 216542
New York, NY 10003-1502
[support@saturncloud.io](mailto:support@saturncloud.io)
[(831) 228-8739](https://saturncloud.io/blog/python-pandas-how-to-remove-nan-and-inf-values/#ZgotmplZ)
##### Platform
[Quick Start](https://saturncloud.io/docs/quickstart/)[API](https://api.saturncloud.io/)[Enterprise](https://saturncloud.io/plans/enterprise/)[Pricing](https://saturncloud.io/plans/saturn_cloud_plans/)
##### Get Started
[Saturn Cloud on Nebius](https://saturncloud.io/partners/nebius/)[Saturn Cloud on AWS](https://saturncloud.io/plans/enterprise/)[Saturn Cloud on GCP](https://saturncloud.io/plans/gcp/)[Saturn Cloud on Azure](https://saturncloud.io/plans/azure/)[Saturn Cloud on Crusoe](https://saturncloud.io/plans/crusoe/)
##### Resources
[Blog](https://saturncloud.io/blog/)[Glossary](https://saturncloud.io/glossary/)[Docs](https://saturncloud.io/docs/)[Customers](https://saturncloud.io/customers/)
##### Company
[Careers](https://saturncloud.io/careers/)[Security](https://saturncloud.io/docs/admin-guide/security/)[Contact](https://saturncloud.io/help/)
© 2026 Saturn Cloud. All rights reserved.
[Terms](https://saturncloud.io/legal/terms-of-service/) · [Privacy](https://saturncloud.io/legal/privacy-policy/) |
| Readable Markdown | As a data scientist or software engineer, you know that working with data can be challenging, especially when dealing with missing or invalid values. In this post, I’ll show you how to use [Python](https://saturncloud.io/glossary/python) [pandas](https://saturncloud.io/glossary/pandas) to remove `NaN` and `-inf` values from your data.
## What are `NaN` and `-inf` values?
`NaN` stands for *Not a Number* and is a special floating-point value used to represent missing or undefined values. `NaN` values can occur when performing mathematical operations on invalid values, such as dividing by zero or taking the square root of a negative number.
`-Inf` stands for negative infinity and is another special floating-point value used to represent values that are too small to be represented by a finite number. `-Inf` values can occur when performing mathematical operations on extremely small values.
`NaN` and `-inf` values can cause problems when performing calculations or statistical analysis on your data. They can skew your results, produce incorrect values, or cause errors in your code. Therefore, it’s often necessary to remove them before proceeding with your analysis.
## How to remove `NaN` and `-inf` values in Python pandas
Python pandas provides several methods for removing `NaN` and `-inf` values from your data. The most commonly used methods are:
- `dropna()`: removes rows or columns with `NaN` or `-inf` values
- `replace()`: replaces `NaN` and `-inf` values with a specified value
- `interpolate()`: fills `NaN` values with interpolated values
### Using `dropna()`
The `dropna()` method removes rows or columns with `NaN` or `-inf` values from your data. By default, it removes all rows with at least one `NaN` or `-inf` value. You can specify the axis parameter to remove columns instead of rows.
```
import pandas as pd
# create a dataframe that contains NaN values
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, None, 15],
'D': [16, 17, 18, 19, 20]
})
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 NaN 19
4 5 10 15.0 20
```
```
# drop rows that contain NaN values
df = df.dropna()
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
4 5 10 15.0 20
```
In this example, the `dropna()` method removes the fourth row from the DataFrame, which contains a None value in column C.
### Using `replace()`
The `replace()` method replaces `NaN` and `-inf` values with a specified value. You can specify the value to replace `NaN` and `-inf` with using the `value` parameter.
```
import pandas as pd
import numpy as np
# create a dataframe that contains NaN and -inf values
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, np.nan, 15],
'D': [16, 17, -np.inf, 19, 20]
})
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 -inf
3 4 -9 NaN 19.0
4 5 10 15.0 20.0
```
```
# replace NaN and -inf values with 0
df = df.replace([np.nan, -np.inf], 0)
print(df)
```
Output:
```
A B C D
0 1 6 11.0 16.0
1 2 -7 12.0 17.0
2 3 8 13.0 0.0
3 4 -9 0.0 19.0
4 5 10 15.0 20.0
```
In this example, the `replace()` method replaces all `NaN` and `-inf` values with 0.
### Using `interpolate()`
The `interpolate()` method fills `NaN` values with interpolated values based on the values of neighboring rows or columns. You can specify the interpolation method to use using the `method` parameter.
```
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [6, -7, 8, -9, 10],
'C': [11, 12, 13, np.nan, 15],
'D': [16, 17, 18, 19, 20]
})
# Interpolate a value to replace NaN based on its neighbors
df = df.interpolate(method='linear')
```
Output:
```
A B C D
0 1 6 11.0 16
1 2 -7 12.0 17
2 3 8 13.0 18
3 4 -9 14.0 19
4 5 10 15.0 20
```
In this example, the `interpolate()` method fills the `NaN` value in column C with an interpolated value `(14)` based on the values of neighboring rows.
## Conclusion
`NaN` and `-inf` values can cause problems when working with data, but Python pandas provides several methods for removing or replacing them. By using the `dropna()`, `replace()`, and `interpolate()` methods, you can clean your data and proceed with your analysis without worrying about invalid values.
Remember to always carefully consider the impact of removing or replacing `NaN` and `-inf` values on your analysis and to document your data cleaning process. |
| Shard | 90 (laksa) |
| Root Hash | 15109777699043858490 |
| Unparsed URL | io,saturncloud!/blog/python-pandas-how-to-remove-nan-and-inf-values/ s443 |