【发布时间】:2019-01-28 23:42:52
【问题描述】:
我正在尝试用 pandas DataFrame 中的另一个字符串(在本例中为空字符串)替换包含数字的字符串。
我尝试了 .replace 方法和正则表达式:
# creating dummy dataframe
data = pd.DataFrame({'A': ['test' for _ in range(5)]})
# the value that should get replaced with ''
data.iloc[0] = 'test5'
data.replace(regex=r'\d', value='', inplace=True)
print(data)
A
0 test
1 test
2 test
3 test
4 test
如您所见,它只替换字符串中的 '5' 而不是整个字符串。
我也尝试使用 .where 方法,但它似乎不适合我的需要,因为我不想替换任何不包含数字的字符串
它应该是这样的:
A
0
1 test
2 test
3 test
4 test
【问题讨论】:
标签: python python-3.x string pandas dataframe