简体   繁体   English

获取行中每个值的最常见值 - pandas df

[英]Get most common value for each value in row - pandas df

This may be a duplicate please let me know.这可能是重复的,请告诉我。

I have a pandas df like this:我有一个像这样的 pandas df:

id ID name姓名 Common常见的
One A一个
One A一个
One A一个
One B
Two C C

I'd like to output something like this:我想 output 是这样的:

Where the most common name for each id is placed in the common column.每个 id 最常用的名称放在 common 列中。

id ID name姓名 Common常见的
One A一个 A一个
One A一个 A一个
One A一个 A一个
One B A一个
Two C C C C

I've tried this but at this point i'm throwing darts in the dark我已经尝试过了,但此时我正在黑暗中投掷飞镖

df.groupby(['id', 'name']).agg(lambda x:x.value_counts().index[0])

This works:这有效:

df['Common'] = df.groupby('id')['name'].transform(lambda x: x.mode()[0])

Output: Output:

>>> df
    id name Common
0  One    A      A
1  One    A      A
2  One    A      A
3  One    B      A
4  Two    C      C

A longer process is to pivot and map:一个较长的过程是pivot和map:

df.assign(Common = df.id.map(df.pivot(None, 'id', 'name').mode().T.squeeze()))
 
    id name Common
0  One    A      A
1  One    A      A
2  One    A      A
3  One    B      A
4  Two    C      C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM