为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2023-10-26 14:33:36
如果性能不是一个大问题,您可以编写一个按行方法返回出现多次的值: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
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

让我们使用DataFrame.assign:


df.assign(**s)

   a  b  c  d  e

0  1  2  3  7  8

1  4  5  6  7  8


查看完整回答
反对 回复 2023-10-26
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信