【问题标题】:How to replace values in one column with string containing "xxx", with value in another column如何将一列中的值替换为包含 \"xxx\" 的字符串,并替换为另一列中的值
【发布时间】:2023-01-21 21:52:25
【问题描述】:

如果 Col1 包含字符串“Local”,我试图用 Col2 中相应的行值替换 Col1 中的值。

我已经尝试了 3 种变体,但似乎都不起作用。

df = df['Col1'].replace(['Col1'].str.contains("Local"),["Col2"])

&

df.loc[df['Col1'].isin(['Local']), 'Col1'] = ['Col2']

&

df3['Client Name_x']=df3['Client Name_y'].where(df3['Client Name_x'].isin(['Local']),df3['Client Name_x'])

这些代码都没有达到预期的结果......

【问题讨论】:

    标签: python pandas replace where-clause


    【解决方案1】:

    我认为你可以使用:

    df['Col1'] = df['Col1'].mask(df['Col1'].str.contains('Local'), df['Col2'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2019-03-28
      • 2021-11-09
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2015-06-27
      相关资源
      最近更新 更多