Python | Pandas dataframe.first_valid_index()
Last Updated :
19 Nov, 2018
Improve
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
Python3
Now apply the
Python3 1==
Output :
Notice, there is
Python3
Output :
As we can see in the dataframe, the first two rows are having only
Python3
Now apply the
Python3 1==
Output :
Output is 2 because 0th and 1st indexes are having null value.
dataframe.first_valid_index()
function returns index for first non-NA/null value in the dataframe. In the case of Pandas Series, the first non-NA/null index is returned. In the case of pandas Dataframe, that index is returned which has even a single non-NA/null value.
Note : If all elements are non-NA/null, returns None. Also returns None for empty DataFrame
Syntax: DataFrame.first_valid_index() Returns : scalar : type of indexExample #1: Use
first_valid_index()
function to find the first non-NA/null index in the dataframe.
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[None, None, 2, 4, 5],
"B":[5, None, None, 44, 2],
"C":[None, None, None, 1, 5]})
# Print the dataframe
df

first_valid_index()
function.
# applying first_valid_index() function
df.first_valid_index()

non-Na
value in the second column of the first row. so the output is 0, indicating that 0th index contain a non-NA
value.
Example #2: Use first_valid_index()
function to find the first non-NA/null index in the dataframe.
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[None, None, 2, 4, 5],
"B":[None, None, None, 44, 2],
"C":[None, None, None, 1, 5]})
# applying first_valid_index() function
df.first_valid_index()

NA
values. so, the output is 2
Example #3: Use first_valid_index()
function to find the first non-NA/null index in a series.
# importing pandas as pd
import pandas as pd
# Creating the series
ser = pd.Series([None, None, "sam", "alex", "sophia", None])
# Print the series
ser

first_valid_index()
function.
# applying first_valid_index() function
ser.first_valid_index()
