
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
To return a Series containing counts of unique values from Index object considering NaN values as well with the index.value_counts() method. Set the parameter dropna with value False.
At first, import the required libraries -
import pandas as pd import numpy as np
Creating Pandas index with some NaN values as well −
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
Display the Pandas index −
print("Pandas Index...\n",index)
Count of unique values using value_counts(). Considering NaN as well using the "False" value of the "dropna" parameter −
index.value_counts(dropna=False)
Example
Following is the code −
import pandas as pd import numpy as np # Creating Pandas index with some NaN values as well index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30]) # 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) # count of unique values using value_counts() # considering NaN as well using the "False" value of the "dropna" parameter print("\nGet the count of unique values with NaN...\n",index.value_counts(dropna=False))
Output
This will produce the following output −
Pandas Index... Float64Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64') Number of elements in the index... 9 The dtype object... float64 Get the count of unique values with NaN... NaN 3 50.0 2 10.0 1 70.0 1 90.0 1 30.0 1 dtype: int64
- Related Articles
- Python Pandas - Return a Series containing counts of unique values from Index object
- Python Pandas - Return a Series containing counts of unique values from Index object sorted in Ascending Order
- Python Pandas - Return Index without NaN values
- Python Pandas - Return unique values in the index
- Python Pandas - Fill NaN values with the specified value in an Index object
- Python Pandas - Return a list of the Index values
- Python Pandas - Return number of unique elements in the Index object
- Python Pandas - Check if the index has unique values
- Python Pandas - Return Index with duplicate values removed
- Python Pandas - Get unique values from a column
- How does pandas series argsort handles nan values?
- Python Pandas - Return the memory usage of the Index values
- Python Pandas - Return Index with duplicate values completely removed
- Python Pandas – Find unique values from a single column
- Python Pandas – Find unique values from multiple columns

Advertisements