• Post author:
  • Post category:Pandas
  • Post last modified:March 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.

1. 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()]

2. 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)

2.1 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.

2.2 Return Value of dropna()

It returns the pandas Series without NaN values.

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure that is available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other Python objects. We can access each element in the Series with the help of corresponding default indices.

Note : Series data structure is the same as the NumPy array data structure but only one difference is that array indices are integers and start with 0, whereas in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.

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

4. 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

5. 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.

6. 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)

Frequently Asked Questions 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, you have learned how to remove NaN values from Series in Pandas by using Series.dropna(), and Series.isnull() functions with examples.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.