Find mode in panda Dataframe

Question:

Find mode among all values in c1_ind and c2_ind . I don’t want to mode along each column.

import pandas as pd
import numpy as np
from scipy.stats import mode


list =[{"col1":123,"C1_IND    ":"Rev","C2_IND":"Hold"},
{"col1":456,"C1_IND    ":"Hold","C2_IND":"Rev"},
{"col1":123,"C1_IND    ":"Hold","C2_IND":"Service"},
{"col1":1236,"C1_IND    ":"Man","C2_IND":"Man"}]

df = pd.DataFrame.from_dict(list)
print(df)

For another example Find mode among all values in c1_ind and c3_ind


import pandas as pd
import numpy as np
from scipy.stats import mode


list =[{"col1":123,"C1_IND    ":"Rev","C2_IND":"Hold","C3_IND":"Hold"},
{"col1":456,"C1_IND    ":"Hold","C2_IND":"Rev","C3_IND":"Rev"},
{"col1":123,"C1_IND    ":"Hold","C2_IND":"Service","C3_IND":"Service"},
{"col1":1236,"C1_IND    ":"Man","C2_IND":"Man","C3_IND":"Man"}]

df = pd.DataFrame.from_dict(list)
print(df)
Asked By: pbh

||

Answers:

You can use filter to select the columns of interest (here by regex), then stack and finally use mode.:

df.filter(regex='Cd+_IND').stack().mode().iloc[0]

NB. If there are several modes, we only keep one. If you want all, remove the iloc[0], optionally replacing with squeeze.

Output: 'Hold'

Answered By: mozway
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.