gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:19:58 24 4
gpt4 key购买 nike

这是我的 Pandas 数据框

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

有人能帮忙吗?

最佳答案

以下作品:

df['Description'] = df["Description"].str.replace(r'^\w+:\w+', '')


>>> df
Description ID Date
0 Here comes the first row id_112 2018/03/02
1 Here comes the second row id_115 2018/03/02
2 Here comes the third row id_234 2018/03/02

关于python - 从列中删除带冒号的单词 - 为什么它不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49265699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com