Рассчитайте частоту каждого уникального значения серии Pandas

Опубликовано: 27 Марта, 2022

Давайте посмотрим, как найти частоту каждого уникального значения серии Pandas. Мы будем использовать функцию value_counts () для выполнения этой задачи.

Example 1 :

# importing the module
import pandas as pd
  
# creating the series
s = pd.Series(data = [2, 3, 4, 5, 5, 6
                      7, 8, 9, 5, 3])
  
# displaying the series
print(s)
  
# finding the unique count
print(s.value_counts())

Output :

Output:

Example 2 :

# importing the module
import pandas as pd
  
# creating the series
s = pd.Series(np.take(list("0123456789"), 
              np.random.randint(10, size = 40)))
  
# displaying the series
print(s)
  
# finding the unique count
s.value_counts()

Output :

Output:

Example 3 :

# importing pandas as pd 
import pandas as pd 
    
# creating the Series 
sr = pd.Series(["Mumbai", "Pune", "Agra", "Pune"
                "Goa", "Shimla", "Goa", "Pune"]) 
  
# displaying the series 
print(sr) 
  
# finding the unique count
sr.value_counts()

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

Next
Getting frequency counts of a columns in Pandas DataFrame
Recommended Articles
Page :
Article Contributed By :
yash41997
@yash41997
Vote for difficulty
Article Tags :
  • Python pandas-series
  • Python-pandas
  • Python
Report Issue
Python

РЕКОМЕНДУЕМЫЕ СТАТЬИ