首页 > 解决方案 > 使用 str 替换为 pandas 系列添加空间

问题描述

我一定是误解了如何使用替换。

输入

          box
0      11M000
1  11M000(MU)

期望的输出

          box
0      11M000
1  11M000 (MU)

代码

import pandas as pd
data={'box':['11M000','11M000(MU)']}
df = pd.DataFrame(data)
df['box'] = df['box'].str.replace('.(MU)'," (MU)", regex=True)

我现在得到什么

0          11M000
1    11M000 (MU))

标签: pythonregexpandas

解决方案


df['box'] = df['box'].str.replace('('," (")

推荐阅读