从列中删除带冒号的单词为什么不起作用?

2024-09-29 23:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的熊猫数据框

Description                        ID       Date
wa119:d Here comes the first row   id_112   2018/03/02
ax21:3 Here comes the second row   id_115   2018/03/02
bC230:13 Here comes the third row  id_234   2018/03/02

数据类型是

print(df.dtypes)

Description             object
ID                      object
Date                    datetime64[ns]
dtype: object

我想删除那些包含冒号的单词。在本例中,这将是wa119:d、ax21:3和bC230:13,因此我的新数据集应该如下所示:

Description                ID      Date
Here comes the first row   id_112  2018/03/02
Here comes the second row  id_115  2018/03/02
Here comes the third row   id_234  2018/03/02

我尝试了以下方法,但都没有成功:

re.sub('^\\w+:\\w+', '', df["Description"].astype(str))
re.sub('^\\w+:\\w+', '', df["Description"].astype("str"))

我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/fff/PycharmProjects/Test/Test.py", line 17, in <module>
    re.sub('^\\w+:\\w+', '', df["Description"].astype("str"))
  File "C:\Users\fff\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 191, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

有人能帮忙吗?你知道吗


Tags: the数据reiddfdatehereobject

热门问题