假设我有一个pandas数据框,它包含许多行,用于表示产品名称,列用于描述各自的特性。他们添加了一些编号系统,比如1,2,3。,。。。或a),b),c)…或(i),(ii),(iii),。。。等等。现在我想在数据帧中删除它们
df.replace(regex=True, inplace=True, to_replace=r'["(i*)"|i*.|(a-zA-Z).|("("a-zA-z")")]', value=r'')
但代码不起作用。它从我的答案中删除我所有的答案。考虑成为收件人,我可以删除A,B。等,如果我单独给出,即_replace=r'[a.| b.| a.| b.]但是如果给出了模式,它就不起作用了
对于一个或多个带有正则表达式的数据帧,如何从a-Z和I中删除“(I)”、“(ii)”、“(iii)”和“(a)”、“(a)”、“a.”范围
范例
输入
(i) 这头牛有四条腿。牛吃草。牛给我们牛奶
或
这头牛有四条腿。b、 牛吃草。c、 奶牛给我们牛奶
输出
这头牛有四条腿。牛吃草。奶牛给我们牛奶
如果一个
i
字符只能有1次或多次(因此没有罗马数字),您可以使用:模式匹配:
\(?i+\)
匹配可选的(
,然后1+乘以i
字符和)
|
或\b
防止部分匹配的单词边界(?:
非捕获组[A-Za-z]
匹配单个字符a-Za-z|
或\d+
匹配1+个数字)
关闭非捕获组\.
匹配一个点Regex demo
如果你想匹配罗马数字,你可以看到this post
请你试试:
输入:
输出:
regex
^\(?(?:[ivxlcdm]+|[a-zA-Z]+|[0-9]+)[).]
的解释:^
表示字符串的开头李>\(?
匹配零或一个左括号李>(?:[ivxlcdm]+|[a-zA-Z]+|[0-9]+)
可以分解为以下任一种:[ivxlcdm]+
匹配罗马数字的李>[a-zA-Z]+
匹配字母表的李>[0-9]+
匹配数字李>[).]
匹配右括号或点李>相关问题 更多 >
编程相关推荐