Skip to content Skip to sidebar Skip to footer

Pandas Get The Average And Mode Group By Specified Columns

rev_id worker_id toxicity toxicity_score 0 2232.0 723 0 0.0 1 2232.0 4000 0 0.0 2 2232.0 3989 0

Solution 1:

It seems you need groupby with aggregate by aggmean and mode:

df = (df.groupby('rev_id', as_index=False)
        .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.mode()}))

Alternative is value_counts with select first value of index:

df = (df.groupby('rev_id', as_index=False)
        .agg({'toxicity_score':'mean', 'toxicity': lambda x: x.value_counts().index[0]}))

print (df)
   rev_id  toxicity_score  toxicity
0  2232.0             0.4         0
1  4216.0             0.5         0
2  8953.0             0.1         0

Post a Comment for "Pandas Get The Average And Mode Group By Specified Columns"