【问题标题】:Pandas Dataframe replace part of string with value from another columnPandas Dataframe 用另一列的值替换部分字符串
【发布时间】:2022-11-25 18:56:14
【问题描述】:

我在尝试用另一列的值替换字符串时遇到替换问题。 我想用 df['Length'] 替换'Length'。

df["Length"]= df["Length"].replace('Length', df['Length'], regex = True)

下面是我的数据

Input:
**Formula**  **Length**
Length           5
Length+1.5       6
Length-2.5       5
Length           4
5                5

Expected Output:
**Formula**  **Length**
5                5
6+1.5            6
5-2.5            5
4                4
5                5

但是,使用我上面使用的代码,它将替换我的整个单元格而不是仅替换长度。 我得到以下输出: 我发现这是由于使用了 df['column'],如果我使用任何其他字符串,后面的偏移量 (-1.5) 将不会被替换。

**Formula**  **Length**
5                5
6                6
5                5
4                4
5                5

我可以知道其他列的值是否有任何替换方法?

谢谢你。

【问题讨论】:

    标签: python pandas str-replace


    【解决方案1】:

    如果需要用另一列替换,请使用DataFrame.apply

    df["Formula"]= df.apply(lambda x: x['Formula'].replace('Length', str(x['Length'])), axis=1)
    print (df)
      Formula  Length
    0       5       5
    1   6+1.5       6
    2   5-2.5       5
    3       4       4
    4       5       5
    

    或列表理解:

    df["Formula"]= [x.replace('Length', str(y)) for x, y  in df[['Formula','Length']].to_numpy()]
    

    【讨论】:

    • 谢谢 jezrael :) 它工作得很好!感谢您向我介绍 .apply 以及替代解决方案。我真的很感激 :)
    【解决方案2】:

    只是想补充一点,列表理解当然要快得多:

    df = pd.DataFrame({'a': ['aba'] * 1000000, 'c': ['c'] * 1000000})
    
    %timeit df.apply(lambda x: x['a'].replace('b', x['c']), axis=1)
    # 1 loop, best of 5: 11.8 s per loop
    
    %timeit [x.replace('b', str(y)) for x, y in df[['a', 'c']].to_numpy()]
    # 1 loop, best of 5: 1.3 s per loop
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2019-07-25
      • 2019-08-08
      • 1970-01-01
      • 2019-07-16
      • 2020-11-03
      • 1970-01-01
      • 2020-09-08
      • 2019-10-01
      相关资源
      最近更新 更多