How to remove NaN from a Pandas Series? (2024)

In the pandas series constructor, the method called dropna() is used to remove missing values from a series object. And it does not update the original series object with removed NaN values instead of updating the original series object, it will return another series object with updated values.

The parameters of the dropna() method are axis, inplace, and how.

Example 1

# importing packagesimport pandas as pdimport numpy as np# Creating Series objectssr = pd.Series([42, np.nan, 55, 42, np.nan, 73, np.nan, 55, 76, 87], index=list("ABCDEFGHIJ"))print('Series object:',sr)# Remove missing elementsresult = sr.dropna()# display outputprint(result)

Explanation

Initially, we have created a pandas Series with labeled index values and there are some Nan values present in the series object. After creating a pandas series object we have applied the dropna() method to remove missing values.

Output

Series object:A 42.0B NaNC 55.0D 42.0E NaNF 73.0G NaNH 55.0I 76.0J 87.0dtype: float64A 42.0C 55.0D 42.0F 73.0H 55.0I 76.0J 87.0dtype: float64

In the above output block, we can see both initial and resultant series objects. The second series object is the output object with removed missing values.

Example 2

# importing packagesimport pandas as pdimport numpy as npdates = pd.date_range('2021-06-01', periods=10, freq='D')#creating pandas Series with date indexsr = pd.Series([np.nan, 61, 72, 11, np.nan, 24, 56, 30, np.nan, 55], index=dates)print('Series object:',sr)# Remove missing elementsresult = sr.dropna()# display outputprint(result)

Explanation

In the following example, we have created a pandas Series with date range index values and there are some Nan values present in the series object “sr”. After creating a pandas series object we applied the dropna() method to remove those Nan values.

Output

Series object:2021-06-01 NaN2021-06-02 61.02021-06-03 72.02021-06-04 11.02021-06-05 NaN2021-06-06 24.02021-06-07 56.02021-06-08 30.02021-06-09 NaN2021-06-10 55.0Freq: D, dtype: float642021-06-02 61.02021-06-03 72.02021-06-04 11.02021-06-06 24.02021-06-07 56.02021-06-08 30.02021-06-10 55.0dtype: float64

Here we got a new series object with removed Nan values. In the above output block, we can see both initial and resultant series objects. The first object is the initial series, and the second one is the output of the dropna() method.

Related Articles

Kickstart Your Career

Get certified by completing the course

Get Started

How to remove NaN from a Pandas Series? (31)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

How to remove NaN from a Pandas Series? (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6042

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.