Как отобразить наиболее частое значение в серии Pandas?

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

В этой статье наша основная задача - напечатать наиболее частое значение в серии. Мы можем узнать количество вхождений элементов, используя метод value_counts (). Отсюда можно получить доступ к наиболее часто используемому элементу с помощью метода mode ().

Example 1 :

# importing the module
import pandas as pd
   
# creating the series
series = pd.Series(["g", "e", "e", "k", "s"
                    "f", "o", "r"
                    "g", "e", "e", "k", "s"])
print("Printing the Original Series:")
display(series)
  
# counting the frequency of each element
freq = series.value_counts()
print("Printing the frequency")
display(freq)
  
# printing the most frequent element
print("Printing the most frequent element of series")
display(series.mode());

Output :

Example 2 : Replacing the every element except the most frequent element with None.

# importing the module
import pandas as pd
   
# creating the series
series = pd.Series(["g", "e", "e", "k", "s"
                    "f", "o", "r"
                    "g", "e", "e", "k", "s"])
  
# counting the frequency of each element
freq = series.value_counts()
  
# replacing everything else as Other
series[~series.isin(freq .index[:1])] = None
print(series)

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




Previous
Markdown cell in Jupyter notebook
Next
IBM HR Analytics on Employee Attrition & Performance using Random Forest Classifier
Recommended Articles
Page :
Article Contributed By :
hupphurr
@hupphurr
Vote for difficulty
Article Tags :
  • Python Pandas-exercise
  • Python pandas-series
  • Python-pandas
  • Python
Report Issue
Python

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