• Post author:
  • Post category:Pandas
  • Post last modified:November 27, 2024
  • Reading time:14 mins read
You are currently viewing Remove NaN From Pandas Series

In pandas, you can use the <code>Series.dropna() function to remove NaN (Not a Number) values from a Series. It returns new series with the same values as the original but without any NaN values. In this article, I will explain how to remove NaN from Series in Pandas by using dropna() and other methods with examples.

Advertisements

Key Points –

  • Use Pandas’ built-in methods to efficiently identify and eliminate NaN values from Series.
  • Use Pandas functions like isnull() or notnull() to locate NaN values.
  • Use the dropna() method to remove NaN values from a Pandas Series.
  • Use methods like dropna() to remove NaN values from the Series.
  • Specify axis and handling options to customize NaN removal behavior.

Quick Examples of Remove NaN From Series

If you are in a hurry, below are some quick examples of how to remove NaN from the pandas series.


# Quick examples of remove NaN from series

# Example 1: Use dropna() 
# To remove nan values from a pandas series
ser2 = ser.dropna()

# Example 2: Use isnull() 
# To remove nan values from a pandas series
ser2 = ser[~ser.isnull()]

Syntax of Series.dropna() Function

Following is the syntax of Series.dropna() function.


# Syntax of Series.dropna() function
Series.dropna(axis=0, inplace=False, how=None)

Parameter of dropna()

Following are the parameters of the dropna().

  • axis – {0 or ‘index’} by default Value 0: There is only one axis to drop values from.
  • inplace – boolean, default Value: False: If True, do an operation in place and return None.
  • how – str, this optional parameter: Not in use.

Return Value of dropna()

It returns the pandas Series without NaN values.

Create Pandas Series

Now, let’s create pandas series using a list. Note that NaN in pandas and represent by using NumPy np.nan.


import pandas as pd
import numpy as np
 
# Create the Series 
ser = pd.Series(['Java', 'Spark', np.nan, 'PySpark', np.nan,'Pandas','NumPy', np.nan,'Python'])
print("Create series:\n",ser)

Yields below output.

pandas series remove nan

Use dropna() Method to Remove NaN Values From Series

Using dropna() method we can remove the NaN values from the series. Let’s use Series.dropna() method to remove NaN (missing) values from the original Series to get a new series. This method returns a new Series after removing all NAN values.


# Use dropna() 
# To remove nan values from a pandas series
ser2 = ser.dropna()
print(ser2)

Yields below output.


# Output:
0       Java
1      Spark
3    PySpark
5     Pandas
6      NumPy
8     Python
dtype: object

Use isnull() Method to Remove NaN Values From Series

We can also use Series.isnull() on the original Series to get a new Series with only boolean values and the same dimensions as the original. The boolean Series contains True if the value in the original is NaN and False otherwise for each element and use this Series on the original series to remove all NaN values.


# Use isnull() 
# To remove nan values from a pandas series
ser2 = ser[~ser.isnull()]
print(ser2)

Yields the same output as above.

Complete Example For Remove NaN From Series


import pandas as pd
import numpy as np

# Create the Series  
ser = pd.Series(['Java', 'Spark', np.nan, 'PySpark', np.nan,'Pandas','NumPy', np.nan,'Python'])
print(ser)

# Example 1: Use dropna() 
# To remove nan values from a pandas series
ser2 = ser.dropna()
print(ser2)

# Example 2: Use isnull() 
# To remove nan values from a pandas series
ser2 = ser[~ser.isnull()]
print(ser2)

FAQ on Remove NaN From Pandas Series?

How do I check for NaN values in a pandas Series?

You can check for NaN values in a pandas Series using the isna() or isnull() method. For example, the nan_mask will be a boolean Series where each element is True if the corresponding element in the original Series is NaN and False otherwise.

How can I remove NaN values from a pandas Series?

You can remove NaN values from a pandas Series using the dropna() method. For example, data.dropna() returns a new Series (data_without_nan) where NaN values have been removed. If you want to modify the original Series in place, you can use the inplace parameter

Can I remove NaN values in place without creating a new Series?

You can remove NaN values from a pandas Series in place without creating a new Series by using the dropna() method with the inplace parameter set to True.

Is there an alternative way to remove NaN values using boolean indexing?

An alternative way to remove NaN values from a pandas Series is to use boolean indexing. You can use the notna() method to create a boolean mask and then use that mask to filter out the NaN values.

Are there any other options to handle NaN values in pandas?

You can use methods like fillna() to fill NaN values with a specific value or strategy.

Conclusion

In this article, I have explained how to remove NaN values from Series in Pandas by using Series.dropna(), and Series.isnull() functions with examples.

Happy Learning !!

References