Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: how to find the most frequent value of each row?

how to find the most frequent value of each row of a dataframe? For example:

In [14]: df
Out[14]:
   a  b  c
0  2  3  3
1  1  1  2
2  7  7  8

return: [3,1,7]

like image 810
Robin1988 Avatar asked Mar 18 '16 18:03

Robin1988


People also ask

How do you calculate most frequent value in pandas?

value_counts() function can be used to obtain counts, and then the idxmax() function can be used to obtain the value with the most counts.

How do you find the average of each row in pandas?

mean(axis = 1) , it will return the mean of all the indices or rows. So, the number of values return by the function will be equal to the number of rows/indices. df. mean(axis = 1) will return a Pandas series with mean of all the rows.

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

We can find the number of occurrences of elements using the value_counts() method. From that the most frequent element can be accessed by using the mode() method.


1 Answers

try .mode() method:

In [88]: df
Out[88]:
   a  b  c
0  2  3  3
1  1  1  2
2  7  7  8

In [89]: df.mode(axis=1)
Out[89]:
   0
0  3
1  1
2  7

From docs:

Gets the mode(s) of each element along the axis selected. Adds a row for each mode per label, fills in gaps with nan.

Note that there could be multiple values returned for the selected axis (when more than one item share the maximum frequency), which is the reason why a dataframe is returned. If you want to impute missing values with the mode in a dataframe df, you can just do this: df.fillna(df.mode().iloc[0])

like image 53
MaxU - stop WAR against UA Avatar answered Sep 22 '22 18:09

MaxU - stop WAR against UA