batmanforever
batmanforever

Reputation: 155

pandas data frame, select most common value from all rows

I have a data frame and I'd like to take from one Col1 the one most frequent colour. There are a few types of colours: Green, Yellow, Blue, Black.

Col1 has a lot of data. Random colours. For example:

enter image description here

Any idea?

Upvotes: 2

Views: 2343

Answers (3)

batmanforever
batmanforever

Reputation: 155

Ok I found the solution

pd.value_counts(df['colours'].values, sort=True).head(1)

Upvotes: 1

MissBleu
MissBleu

Reputation: 175

you could try something like

import pandas as pd
colordf = { 'colors' : ['Green', 'Yellow', 'Blue', 'Orange', 'Green',
          'Yellow', 'Blue', 'Orange', 'Green', 'Yellow'],
            'numbers' : [1,2,3,4,5,6,7,8,9,8]}
df = pd.DataFrame.from_dict(colordf)
df['colors'].value_counts().head(1)

Upvotes: 0

BENY
BENY

Reputation: 323226

You are looking for mode , data from MissBleu

df.colors.mode()
Out[36]: 
0     Green
1    Yellow
dtype: object

The reason why you have two rather than one : Both of them have frequency 4

Upvotes: 2

Related Questions