2 回答
TA贡献1829条经验 获得超13个赞
让我们尝试Counter获取最常见的元素:
from collections import Counter
def most_common():
for s in df.to_numpy():
k, v = Counter(s).most_common(1)[0]
yield '-' if v == 1 else k
df['Result'] = list(most_common())
col1 col2 col3 Result
0 A A B A
1 A B B B
2 C B C C
3 A B C -
TA贡献1719条经验 获得超6个赞
如果性能不是一个大问题,您可以编写一个按行方法返回出现多次的值:
def appear_more_than_once(s):
cnt = s.value_counts()
if len(cnt) > 0 and cnt.iloc[0] > 1:
return cnt.index[0]
else:
return '-'
df['Result'] = df.apply(appear_more_than_once, axis=1)
print(df)
# col1 col2 col3 Result
#0 A A B A
#1 A B B B
#2 A B C -
https://uscript.co/public/Google_108617488638529745626/python/4e5d36bc.py
添加回答
举报