Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Pandas dataframe.first_valid_index()

Python | Pandas dataframe.first_valid_index()

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 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 index

Example #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


Now apply the first_valid_index() function.




# applying first_valid_index() function 
df.first_valid_index()


Output :

Notice, there is 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()


Output :

As we can see in the dataframe, the first two rows are having only 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


Now apply the first_valid_index() function.




# applying first_valid_index() function 
ser.first_valid_index()


Output :

Output is 2 because 0th and 1st indexes are having null value.

RELATED ARTICLES

Most Popular

Recent Comments