【发布时间】:2020-10-08 14:15:42
【问题描述】:
我创建了一个函数,它对长度为 1045459 的数据帧进行一些文本处理。编译时间似乎比正常情况要长。
这就是我所做的:
def clean_descriptions(text_list):
# First get everything into lowercase
text_list = str(text_list)
for x in text_list:
x = x.lower()
# Remove all instances of 3 characters followed by a number
for substr in re.findall(r'\W([A-Z][A-Z][A-Z]\d)\W', text_list):
text_list = text_list.replace(substr, '')
text_list = text_list.replace('[]','')
# Remove NA
text_list = text_list.replace('NA','')
return text_list
这是我使用函数的方式:
df['short_description'] = clean_descriptions(df['short_description'].tolist())
有没有更有效的方法来做后者?
以下是 short_description 的示例:
PRG2 - stelucie needs help with Radio
[VLR44] vlrd-fc-edg-fw-01-00-01:BGP Status - WARNING [DEEP-DIVE]
[LGB3] lgb3-ar-acc-sw172129.amazon.com:PSU Check
[BFI4] Walk Up Ticket - Other
[FC-OOB]-DMO3 is down [DEEP-DIVE]
【问题讨论】:
-
熊猫?
text_list是什么?列表?数据框?系列?为什么不使用 pandas 内置的正则表达式和 pandas 函数对 df` 进行操作? -
当您可以同时使用
re.sub时,为什么还要调用findall然后replace? -
@PatrickArtner 我编辑了我的问题,但是我正在使用 pandas 数据框,但我可能正在做一些导致它超级慢的事情。
-
我认为我有一个更有效的解决方案来解决您的问题,但是您是否有一些示例输入数据以及我可以测试的所需输出?