 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 