Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove NaN from pandas series

Is there a way to remove a NaN values from a panda series? I have a series that may or may not have some NaN values in it, and I'd like to return a copy of the series with all the NaNs removed.

like image 867
user1802143 Avatar asked Nov 27 '13 06:11

user1802143


People also ask

How do I delete NaN in Pandas?

Use dropna() function to drop rows with NaN / None values in pandas DataFrame. Python doesn't support Null hence any missing data is represented as None or NaN. NaN stands for Not A Number and is one of the common ways to represent the missing value in the data.

How do I remove a value from a series in Pandas?

From a pandas Series a set of elements can be removed using the index, index labels through the methods drop() and truncate(). The drop() method removes a set of elements at specific index locations. The locations are specified by index or index labels.

How do you remove a Null from a series in Python?

Analyze and drop Rows/Columns with Null values in a Pandas series. The dropna() function is used to return a new Series with missing values removed. There is only one axis to drop values from. If True, do operation inplace and return None.

Which statement do you use to eliminate NaN values in a Pandas DataFrame?

Pandas dropna() - Drop Null/NA Values from DataFrame.


1 Answers

>>> s = pd.Series([1,2,3,4,np.NaN,5,np.NaN]) >>> s[~s.isnull()] 0    1 1    2 2    3 3    4 5    5 

update or even better approach as @DSM suggested in comments, using pandas.Series.dropna():

>>> s.dropna() 0    1 1    2 2    3 3    4 5    5 
like image 186
Roman Pekar Avatar answered Sep 21 '22 05:09

Roman Pekar