Pandas DataFrame last_valid_index() Method
In this tutorial, we will learn the Python pandas DataFrame.last_valid_index()
method. By using this method, we can get the index for the last 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.last_valid_index()
.
Syntax
DataFrame.last_valid_index()
Example: The DataFrame.last_valid_index()
Method.
Let's create a DataFrame with null values and get the index of the last non-NA value using the DataFrame.last_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 last non-NA/null value is:",df.last_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 last non-NA/null value is: 5
Example: Getting the index for the last non-NA/null in Pandas
This example is similar to the previous one except dealing with non-null values. Let's create a DataFrame with null values and get the index of the last non-NA value using the DataFrame.last_valid_index()
method. See the below example.
#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],[2, 8, 0],[7, 5, 4],[np.nan,np.nan,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("Index for last non-NA/null value is:",df.last_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 2.0 8.0 0.0
4 7.0 5.0 4.0
5 NaN NaN NaN
Index for last non-NA/null value is: 4
Example: Getting the index for the last non-NA/null value
The DataFrame.
last_valid_index()
method returns None
if all elements are non-NA/null else returns a index value. See the below example.
#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 last non-NA/null value is:",df.last_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 last non-NA/null value is: None
Conclusion
In this tutorial, we learned the Python pandas DataFrame.last_valid_index()
method. We learned the syntax and applying this method to understood the DataFrame.last_valid_index()
method.