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.
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.
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.
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 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”.
By using mode
df.name.mode() Out[712]: 0 alex 1 helen dtype: object
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With