- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove NaN from a Pandas Series?
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.
# importing packages import pandas as pd import numpy as np # Creating Series objects sr = pd.Series([42, np.nan, 55, 42, np.nan, 73, np.nan, 55, 76, 87], index=list("ABCDEFGHIJ")) print('Series object:',sr) # Remove missing elements result = sr.dropna() # display output print(result)
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.
Series object: A 42.0 B NaN C 55.0 D 42.0 E NaN F 73.0 G NaN H 55.0 I 76.0 J 87.0 dtype: float64 A 42.0 C 55.0 D 42.0 F 73.0 H 55.0 I 76.0 J 87.0 dtype: 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.
# importing packages import pandas as pd import numpy as np dates = pd.date_range('2021-06-01', periods=10, freq='D') #creating pandas Series with date index sr = pd.Series([np.nan, 61, 72, 11, np.nan, 24, 56, 30, np.nan, 55], index=dates) print('Series object:',sr) # Remove missing elements result = sr.dropna() # display output print(result)
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.
Series object: 2021-06-01 NaN 2021-06-02 61.0 2021-06-03 72.0 2021-06-04 11.0 2021-06-05 NaN 2021-06-06 24.0 2021-06-07 56.0 2021-06-08 30.0 2021-06-09 NaN 2021-06-10 55.0 Freq: D, dtype: float64 2021-06-02 61.0 2021-06-03 72.0 2021-06-04 11.0 2021-06-06 24.0 2021-06-07 56.0 2021-06-08 30.0 2021-06-10 55.0 dtype: 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
- How to leave nan as nan in the pandas series argsort method?
- How to remove a group of elements from a pandas series object?
- How does pandas series argsort handles nan values?
- How to remove a specified row From the Pandas Series Using Drop() method?
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- How to check whether the Pandas series is having Nan values or not?
- How to remove rows in a Pandas series with duplicate indices?
- How to create a Pandas series from a python dictionary?
- How to create a series from a list using Pandas?
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- How to get few rows from a Series in Pandas?
- How to remove rows from data frame in R that contains NaN?
- Creating a Dataframe from Pandas series
- How to sort a Pandas Series?
- How to plot a bar graph in Matplotlib from a Pandas series?