Signup/Sign In
PUBLISHED ON: MARCH 17, 2021

Pandas DataFrame first_valid_index() Method

In this tutorial, we will learn the Python pandas DataFrame.first_valid_index() method. By using this method, we can get the index for the first non-NA/null value. It returns a scalar that is the type of index. It returns None if all elements are non-NA/null and also returns None for empty DataFrame.

The below shows the syntax of the DataFrame.first_valid_index().

Syntax

DataFrame.first_valid_index()

Example: The DataFrame.first_valid_index() Method

Create the DataFrame with null values and get the index of the first non-NA value using the DataFrame.first_valid_index() method.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[np.nan, np.nan, np.nan],[np.nan, 2,5],[1, 3, 4],[np.nan,3,np.nan],[2, 8, 0],[7, 5, 4]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("Index for first non-NA/null value is:",df.first_valid_index())

Once we run the program we will get the following output.


-----The DataFrame is-----
A B C
0 NaN NaN NaN
1 NaN 2.0 5.0
2 1.0 3.0 4.0
3 NaN 3.0 NaN
4 2.0 8.0 0.0
5 7.0 5.0 4.0
Index for first non-NA/null value is: 1

Example: Find index for the first non-NA/null value using the DataFrame.first_valid_index() Method

This is another example to better understand the first_valid_index() method.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[np.nan, np.nan, np.nan],[np.nan, np.nan, np.nan],[np.nan, np.nan, np.nan],[np.nan,3,np.nan],[2, 8, 0],[7, 5, 4]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("Index for first non-NA/null value is:",df.first_valid_index())

Once we run the program we will get the following output.


-----The DataFrame is-----
A B C
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN 3.0 NaN
4 2.0 8.0 0.0
5 7.0 5.0 4.0
Index for first non-NA/null value is: 3

Example: The DataFrame contains all null Values

The DataFrame.first_valid_index() method returns None if all elements are non-NA/null.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[np.nan, np.nan, np.nan],[np.nan, np.nan, np.nan],[np.nan, np.nan, np.nan],[np.nan, np.nan, np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("Index for first non-NA/null value is:",df.first_valid_index())

Once we run the program we will get the following output.


-----The DataFrame is-----
A B C
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
Index for first non-NA/null value is: None

Conclusion

In this tutorial, we learned the Python pandas DataFrame.first_valid_index() method. We learned the syntax and by applying this method on the DataFrame we solved examples and understood the DataFrame.first_valid_index() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.