Remove NaN from pandas series Remove NaN from pandas series python python

Remove NaN from pandas series


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

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

>>> s.dropna()0    11    22    33    45    5


A small usage of np.nan ! = np.nan

s[s==s]Out[953]: 0    1.01    2.02    3.03    4.05    5.0dtype: float64

More Info

np.nan == np.nanOut[954]: False


If you have a pandas serie with NaN, and want to remove it (without loosing index):

serie = serie.dropna()

# create data for exampledata = np.array(['g', 'e', 'e', 'k', 's']) ser = pd.Series(data)ser.replace('e', np.NAN)print(ser)0      g1    NaN2    NaN3      k4      sdtype: object# the codeser = ser.dropna()print(ser)0    g3    k4    sdtype: object