我有这个数据框:
dfsupport = pd.DataFrame({'Date': ['8/12/2020','8/12/2020','13/1/2020','24/5/2020','31/10/2020','11/7/2020','11/7/2020'],
'Category': ['Table','Chair','Cushion','Table','Chair','Mats','Mats'],
'Sales': ['1 table','3chairs','8 cushions','3Tables','12 Chairs','12Mats','4Mats'],
'Paid': ['Yes','Yes','Yes','Yes','No','Yes','Yes',],
'Amount': ['93.78','$51.99','44.99','38.24','£29.99','29 21 only','18']
})
以表格形式显示如下:
Date Category Sales Paid Amount
0 8/12/2020 Table 1 table Yes 93.78
1 8/12/2020 Chair 3chairs Yes $51.99
2 13/1/2020 Cushion 8 cushions Yes 44.99
3 24/5/2020 Table 3Tables Yes 38.24
4 31/10/2020 Chair 12 Chairs No £29.99
5 11/7/2020 Mats 12Mats Yes 29 21 only
6 11/7/2020 Mats 4Mats Yes 18
我想删除上面的两个字符串元素。我学会了如何成功地将 $ 和 £ 替换为:
patternv='|'.join(re.escape(x) for x in ['$', '£'])
dfsupport['Amount'] = dfsupport['Amount'].str.replace(patternv,regex=True)
我现在想要替换“金额”列中包含“29 21 only”的条目。我的尝试是:
patterns="{r'(\d{1,})\s(\d{1,2})\D+' : r'\1 \2'}"
dfsupport['Amount']=dfsupport['Amount'].str.replace(patterns,regex=True)
但是我的尝试导致了错误:
Traceback (most recent call last):
File "/home/cloud/code/learning/howmany.py", line 160, in <module>
dfsupport['Amount'] = dfsupport['Amount'].str.replace(patternv,regex=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/cloud/.venv/lib/python3.12/site-packages/pandas/core/strings/accessor.py", line 136, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: StringMethods.replace() missing 1 required positional argument: 'repl'
我该如何解决?
我应该补充一点,我正在寻求输出为“29.21”
您缺少第二个(强制)参数
str.replace
:似乎您正在尝试在 中使用字典
patterns
,但这并不像您那样工作,您必须传递一个正则表达式。如果你想传递字典,请使用
replace
(不带str.
):输出: