ℹ️ 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.5 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://www.includehelp.com/python/pandas-how-to-remove-nan-and-inf-values.aspx |
| Last Crawled | 2026-03-27 04:53:06 (13 days ago) |
| First Indexed | 2023-02-13 12:55:54 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python - Pandas: How to remove nan and -inf values? |
| Meta Description | In this tutorial, we are going to learn how to remove nan and -inf values in Python Pandas? |
| Meta Canonical | null |
| Boilerpipe Text | Home
»
Python
»
Python Programs
Learn, how to remove nan and -inf values in Python Pandas?
By
Pranit Sharma
Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame.
DataFrames
are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose that we are given a dataframe that contains several rows and columns with
nan
and
-inf
values too. We need to remove these
nans
and
-inf
values for better data analysis.
Removing nan and -inf values
For this purpose, we will use
pandas.DataFrame.isin()
and check for rows that have any with
pandas.DataFrame.any()
. Finally, we will use the boolean array to slice the dataframe.
Let us understand with the help of an example,
Python program to remove nan and -inf values from pandas dataframe
# Importing pandas package
import
pandas
as
pd
# Import numpy
import
numpy
as
np
from
numpy
import
inf
# Creating a dataframe
df
=
pd
.
DataFrame(data
=
{
'X'
: [
1
,
1
,np
.
nan],
'Y'
: [
8
,
-
inf,
7
],
'Z'
: [
5
,
-
inf,
4
],
'A'
: [
3
,np
.
nan,
7
]})
# Display the DataFrame
print
(
"Original DataFrame:
\n
"
,df,
"
\n\n
"
)
# Removing nan and -inf
res
=
df[
~
df
.
isin([np
.
nan,
-
np
.
inf])
.
any(
1
)]
# Display Result
print
(
"Result:
\n
"
,res)
Output
The output of the above program is:
Python Pandas Programs »
Advertisement
Advertisement |
| Markdown | null |
| Readable Markdown | [Home](https://www.includehelp.com/default.aspx) » [Python](https://www.includehelp.com/python/default.aspx) » [Python Programs](https://www.includehelp.com/python/programs.aspx)
Learn, how to remove nan and -inf values in Python Pandas? By [Pranit Sharma](https://www.includehelp.com/Members/Pranit-Sharma.aspx) Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. [DataFrames](https://www.includehelp.com/python/pandas-dataframe-in-python.aspx) are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
## Problem statement
Suppose that we are given a dataframe that contains several rows and columns with *nan* and *\-inf* values too. We need to remove these *nans* and *\-inf* values for better data analysis.
## Removing nan and -inf values
For this purpose, we will use pandas.DataFrame.isin() and check for rows that have any with pandas.DataFrame.any(). Finally, we will use the boolean array to slice the dataframe.
Let us understand with the help of an example,
## Python program to remove nan and -inf values from pandas dataframe
```
# Importing pandas package
import pandas as pd
# Import numpy
import numpy as np
from numpy import inf
# Creating a dataframe
df = pd.DataFrame(data={'X': [1,1,np.nan], 'Y': [8,-inf,7], 'Z': [5,-inf,4],'A': [3,np.nan,7]})
# Display the DataFrame
print("Original DataFrame:\n",df,"\n\n")
# Removing nan and -inf
res = df[~df.isin([np.nan, -np.inf]).any(1)]
# Display Result
print("Result:\n",res)
```
### Output
The output of the above program is:

[Python Pandas Programs »](https://www.includehelp.com/python/pandas-programs.aspx)
Advertisement
Advertisement |
| Shard | 106 (laksa) |
| Root Hash | 1184066710662109106 |
| Unparsed URL | com,includehelp!www,/python/pandas-how-to-remove-nan-and-inf-values.aspx s443 |