get first not null value from column dataframe

Solutions on MaxInterview for get first not null value from column dataframe by the best coders in the world

showing results for - "get first not null value from column dataframe"
Niklas
27 Jun 2019
1s = pd.Series([np.nan,2,np.nan])
2print (s)
30    NaN
41    2.0
52    NaN
6dtype: float64
7
8print (s.first_valid_index())
91
10
11print (s.loc[s.first_valid_index()])
122.0
13
14# If your Series contains ALL NaNs, you'll need to check as follows:
15
16s = pd.Series([np.nan, np.nan, np.nan])
17idx = s.first_valid_index()  # Will return None
18first_valid_value = s.loc[idx] if idx is not None else None
19print(first_valid_value)
20None
21