Built with Tutify.io

    AIStudyLabs

    Remove NaN From Pandas Series

    Remove NaN From Pandas Series

    In pandas, you can use the 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.

    Key Points –

    <a id="h-quick-examples-of-remove-nan-from-series"></a>

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

    <a id="h-syntax-of-series-dropna-function"></a>

    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)
    

    <a id="h-parameter-of-dropna"></a>

    Parameter of dropna()

    Following are the parameters of the dropna().

    <a id="h-return-value"></a>

    Return Value

    This function returns the pandas Series without NaN values.

    <a id="h-create-pandas-series"></a>

    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.

    <a id="h-use-dropna-method-to-remove-nan-values-from-series"></a>

    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
    

    <a id="h-use-isnull-method-to-remove-nan-values-from-series"></a>

    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.

    <a id="h-complete-example-for-remove-nan-from-series"></a>

    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)
    

    <a id="h-faq-on-remove-nan-from-pandas-series"></a>

    FAQ on Remove NaN From 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.

    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

    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.

    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.

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

    <a id="h-conclusion"></a>

    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 !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References