I have a dataframe that lookS like this:
var1 var2 var3 var4
a a a b
c c b d
e e f g
g a a z
g a a g
w w w w
what I want to do is to identify the most frequent value for each row and count the number of times it appears, in this case I'd get
var1 var2 var3 var4 frq
a a a b 3
c c b d 2
e e f g 2
g a a z 2
g a a g 2
w w w w 4
I was thinking to use something like pd.get_dummies but there would be too many dummies as each var1, var2 etc may assume quite a few different values
Another way is to apply with axis=1:
df['frq'] = df.apply(lambda x: x.value_counts().iloc[0], axis=1)
Or use stack
and groupby
:
df['frq'] = df.stack().groupby(level=0).value_counts().max(level=0)