Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas get the most frequent values of a column

i have this dataframe:

0 name data 1 alex asd 2 helen sdd 3 alex dss 4 helen sdsd 5 john sdadd 

so i am trying to get the most frequent value or values(in this case its values) so what i do is:

dataframe['name'].value_counts().idxmax() 

but it returns only the value: Alex even if it Helen appears two times as well.

like image 653
aleale Avatar asked Feb 02 '18 20:02

aleale


People also ask

How do you get the most frequent value in pandas?

To get the most frequent value of a column we can use the method mode . It will return the value that appears most often. It can be multiple values.

How do I find the most common values in a column in python?

To sum the number of times an element or number appears, Python's value_counts() function is used. The mode() method can then be used to get the most often occurring element.

What does value_counts () do in pandas?

Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element.

How do you count occurrences of specific value in pandas column?

How do you Count the Number of Occurrences in a data frame? To count the number of occurrences in e.g. a column in a dataframe you can use Pandas value_counts() method. For example, if you type df['condition']. value_counts() you will get the frequency of each unique value in the column “condition”.


2 Answers

By using mode

df.name.mode() Out[712]:  0     alex 1    helen dtype: object 
like image 190
BENY Avatar answered Sep 21 '22 18:09

BENY


To get the n most frequent values, just subset .value_counts() and grab the index:

# get top 10 most frequent names n = 10 dataframe['name'].value_counts()[:n].index.tolist() 
like image 22
Jared Wilber Avatar answered Sep 22 '22 18:09

Jared Wilber