【发布时间】:2019-11-09 21:52:54
【问题描述】:
我正在处理数据框。 在“日期”列中,出于某种原因,某些日期在月份之后的括号中带有一个数字。
即2010 年 4 月 5 日 (2)
我想变成:2010 年 4 月 5 日
有没有办法去掉列中所有行的空格和括号部分?
这是我唯一尝试过的,但我对使用正则表达式很陌生,所以我不知道如何解决它:
re.sub(" (.)", "", df['Date'])
这是我得到的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-103-06833d51a3a0> in <module>
1 import re
----> 2 re.sub(" (.)", "", df['Date'])
~/anaconda3/lib/python3.7/re.py in sub(pattern, repl, string, count, flags)
190 a callable, it's passed the Match object and must return
191 a replacement string to be used."""
--> 192 return _compile(pattern, flags).sub(repl, string, count)
193
194 def subn(pattern, repl, string, count=0, flags=0):
TypeError: expected string or bytes-like object
提前致谢!
【问题讨论】: