【问题标题】:Replace string with corresponding string from another column with pandas用 pandas 的另一列中的相应字符串替换字符串
【发布时间】:2019-06-06 15:35:03
【问题描述】:

我有一个名为 df 的数据框,看起来像这样:

pd.DataFrame({
    'column1' : ['client#1 is #name#', 'client#2 is #name#'], 
    'column2': ['josh', 'max']}
)

              column1 column2
0  client#1 is #name#    josh
1  client#2 is #name#     max

我正在尝试将 column1 中的短语“#name”替换为 column2 的值。我希望最终结果如下所示:

我尝试了以下几种方法:

df['column1'] = df['column1'].replace(["#name#"], df['column2'])

但我不确定如何获取 column1 中的特定短语 '#name#' 并将其替换为 column2 的值。任何有关如何解决此问题的建议将不胜感激!

【问题讨论】:

    标签: python string pandas dataframe replace


    【解决方案1】:

    如果是字符串,并且没有 NaN,我建议在速度列表理解中调用 str.replace

    df['column1'] = [
        x.replace('#name#', y) for x, y in zip(df.column1, df.column2)]
    
    df
                column1 column2
    0  client#1 is josh    josh
    1   client#2 is max     max
    

    为什么列表推导值得用于字符串操作?你可以在For loops with pandas - When should I care?阅读更多内容。


    您可以考虑的另一个有趣的选项是str.replaceiter

    it = iter(df.column2)
    df['column1'] = df.column1.str.replace('#name#', lambda x: next(it))
    
    df
                column1 column2
    0  client#1 is josh    josh
    1   client#2 is max     max
    

    应该可以很好地处理 NaN 和混合数据类型(但会慢一些)。


    @Vaishali 提供的更简单的replace 选项,如果“#name#”子字符串始终位于字符串末尾,则该选项将起作用。

    df['column1'] = df.column1.add(df.column2).str.replace('#name#', '')
    df
                column1 column2
    0  client#1 is josh    josh
    1   client#2 is max     max
    

    【讨论】:

    • 感谢@coldspeed,这真的有助于并阐明我想要做什么!
    • @user3116949 忠告,尽量确保您的问题符合网站指南。这意味着所有数据都应在您的问题中以可运行代码的形式出现。
    • 或者干脆 df.column1.add(df.column2).str.replace('#name#', '') :)
    • @Vaishali 啊,这是一个不错的选择,假设“#name#”总是在字符串的末尾。
    猜你喜欢
    • 2017-01-28
    • 2021-08-02
    • 1970-01-01
    • 2018-03-30
    • 2021-10-27
    • 2018-03-02
    • 2016-11-28
    相关资源
    最近更新 更多