Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - Show which entries in a Pandas Index are not NA
To show which entries in a Pandas Index are not NA, use the index.notna() method. This method returns a boolean array where True indicates non-NA values and False indicates NA values.
Syntax
index.notna()
Creating an Index with NA Values
First, let's create a Pandas Index containing some NaN values ?
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values
index = pd.Index([5, 65, np.nan, 17, 75, np.nan])
# Display the Pandas index
print("Pandas Index...\n", index)
Pandas Index... Float64Index([5.0, 65.0, nan, 17.0, 75.0, nan], dtype='float64')
Using notna() Method
The notna() method returns a boolean array showing which entries are not-NA ?
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values
index = pd.Index([5, 65, np.nan, 17, 75, np.nan])
# Show which entries in a Pandas index are not-NA
# Return True for non-NA entries
result = index.notna()
print("Check which entries are not-NA...\n", result)
# Count of non-NA entries
print("\nNumber of non-NA entries:", result.sum())
Check which entries are not-NA... [ True True False True True False] Number of non-NA entries: 4
Complete Example
Here's a comprehensive example showing index information and NA checking ?
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values
index = pd.Index([5, 65, np.nan, 17, 75, np.nan])
# Display the Pandas index
print("Pandas Index...\n", index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n", index.size)
# Return the dtype of the data
print("\nThe dtype object...\n", index.dtype)
# Show which entries in a Pandas index are not-NA
# Return True for non-NA entries
print("\nCheck which entries are not-NA...\n", index.notna())
Pandas Index... Float64Index([5.0, 65.0, nan, 17.0, 75.0, nan], dtype='float64') Number of elements in the index... 6 The dtype object... float64 Check which entries are not-NA... [ True True False True True False]
Conclusion
Use index.notna() to identify non-NA entries in a Pandas Index. The method returns a boolean array where True indicates valid values and False indicates missing values.
Advertisements
