Как отобразить наиболее частое значение в серии Pandas?
Опубликовано: 27 Марта, 2022
В этой статье наша основная задача - напечатать наиболее частое значение в серии. Мы можем узнать количество вхождений элементов, используя метод value_counts (). Отсюда можно получить доступ к наиболее часто используемому элементу с помощью метода mode ().
Example 1 :
# importing the moduleimport pandas as pd # creating the seriesseries = 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 elementfreq = series.value_counts()print("Printing the frequency")display(freq) # printing the most frequent elementprint("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 moduleimport pandas as pd # creating the seriesseries = pd.Series(["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"]) # counting the frequency of each elementfreq = series.value_counts() # replacing everything else as Otherseries[~series.isin(freq .index[:1])] = Noneprint(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