Remove leading NaN in pandas
Here is another method using pandas methods only:
In [103]:
s = pd.Series([np.nan, np.nan, np.nan, 1, 2, np.nan, 3])
first_valid = s[s.notnull()].index[0]
s.iloc[first_valid:]
Out[103]:
3 1
4 2
5 NaN
6 3
dtype: float64
So we filter the series using notnull
to get the first valid index. Then use iloc
to slice the series
EDIT
As @ajcr has pointed out it is better to use the built-in method first_valid_index
as this does not return a temp series which I'm using to mask in the above answer, additionally using loc
uses the index label rather than iloc
which uses ordinal position which works for the general case where the index is not an int64Index:
In [104]:
s = pd.Series([np.nan, np.nan, np.nan, 1, 2, np.nan, 3])
s.loc[s.first_valid_index():]
Out[104]:
3 1
4 2
5 NaN
6 3
dtype: float64
Find first non-nan index
To find the index of the first non-nan item
s = pd.Series([np.nan, np.nan, np.nan, 1, 2, np.nan, 3])
nans = s.apply(np.isnan)
first_non_nan = nans[nans == False].index[0] # get the first one
Output
s[first_non_nan:]
Out[44]:
3 1
4 2
5 NaN
6 3
dtype: float64