Cranjis
Cranjis

Reputation: 1960

pandas dataframe get the value with most occurence per row (Python2)

I have the dataframe

df =A B B A B
    B B B B A
    A A A B B
    A A B A A

And I want to get a vector with the element the appeared the most, per row. So here I will get [B,B,A,A]

What is the best way to do it? In Python2

Upvotes: 1

Views: 35

Answers (2)

BENY
BENY

Reputation: 323316

Let us using mode

df.T.mode()
   0  1  2  3
0  B  B  A  A

Upvotes: 1

Igor OA
Igor OA

Reputation: 397

You can get your vector v with most appearing values with

v = [_[1].value_counts().idxmax() for _ in df.iterrows()].

Be careful when you have multiple elements that occur the most.

Upvotes: 0

Related Questions