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

How to find the most common value of each row of data? For instance:

In [14]: df Out[14]: abc 0 2 3 3 1 1 1 2 2 7 7 8 

Refund: [3,1,7]

+5
source share
1 answer

try it . mode () method:

 In [88]: df Out[88]: abc 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 the docs:

Gets the mode of each item along the selected axis. Adds a line for each mode to the label fills the blanks with nan.

Note that there may be several values โ€‹โ€‹returned for the selected one (when more than one position shares the maximum frequency), which is the reason the data frame is returned. If you want to dispute the missing values โ€‹โ€‹with the mode in the df data frame, you can simply do this: df.fillna (df.mode (). Iloc [0])

+12
source

Source: https://habr.com/ru/post/1245338/


All Articles