将匹配列名称中的 DataFrame 值替换为 Series 的键

如果性能不是一个大问题,您可以编写一个按行方法返回出现多次的值:


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


跃然一笑
浏览 122回答 1
1回答

幕布斯7119047

让我们使用DataFrame.assign:df.assign(**s)   a  b  c  d  e0  1  2  3  7  81  4  5  6  7  8
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python