小编Js *_*fzr的帖子

Pandas Dataframe 用另一列的值替换部分字符串

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

df["Length"]= df["Length"].replace('Length', df['Length'], regex = True)
Run Code Online (Sandbox Code Playgroud)

下面是我的数据

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
Run Code Online (Sandbox Code Playgroud)

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

**Formula**  **Length**
5                5
6                6
5                5
4                4
5                5
Run Code Online (Sandbox Code Playgroud)

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

谢谢你。

python str-replace pandas

7
推荐指数
1
解决办法
1949
查看次数

标签 统计

pandas ×1

python ×1

str-replace ×1