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

在两个 pandas 列之间交换值

在两个 pandas 列之间交换值

慕斯709654 2023-06-27 18:28:36
如何根据定义的条件交换数据框中的值?鉴于:DF[['Exchange','predictions']]Exchange    predictions0   PINK    <UNK>1   PINK    <UNK>2   PINK    <UNK>3   PINK    <UNK>4   PINK    <UNK>... ... ...490541  NASDAQ  PINK490542  NaN PINK490543  NASDAQ  PINK490544  NaN PINK490545  NASDAQ  PINK仅当 Exchange 值为 NaN 并且预测值不是 < UNK > 时,我才希望将 Exchange 替换为预测中的值。
查看完整描述

1 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

让我们尝试fillna部分条件Series


df.Exchange.fillna(df.loc[df['predictions'].ne('<UNK>'), 'predictions'], inplace=True)

df

Out[210]: 

       Exchange predictions

0          PINK       <UNK>

1          PINK       <UNK>

2          PINK       <UNK>

3          PINK       <UNK>

4          PINK       <UNK>

         ...         ...

490541   NASDAQ        PINK

490542     PINK        PINK

490543   NASDAQ        PINK

490544     PINK        PINK

490545   NASDAQ        PINK


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

添加回答

举报

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