【发布时间】:2019-12-14 20:43:58
【问题描述】:
我的脚本从“@#$!”等不需要的字符串中清除数组和其他东西。 该脚本按预期工作,但是当 excel 行大小很大时,它的速度非常慢。
如果它可以加快速度,我尝试使用 numpy,但我不太熟悉它,所以我可能使用不正确。
xls = pd.ExcelFile(path)
df = xls.parse("Sheet2")
TeleNum = np.array(df['telephone'].values)
def replace(orignstr): # removes the unwanted string from numbers
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
return orignstr
for UncleanNum in tqdm(TeleNum):
newnum = replace(str(UncleanNum)) # calling replace function
df['telephone'] = df['telephone'].replace(UncleanNum, newnum) # store string back in data frame
我还尝试删除该方法,如果有帮助,只需将其作为一个代码块放置,但速度保持不变。
for UncleanNum in tqdm(TeleNum):
orignstr = str(UncleanNum)
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
print(orignstr)
df['telephone'] = df['telephone'].replace(UncleanNum, orignstr)
TeleNum = np.array(df['telephone'].values)
目前运行 200,000 个 excel 文件的脚本速度约为 70it/s,大约需要一个小时才能完成。这不是很好,因为这只是众多功能之一。
我在 python 方面不太先进。我只是在编写脚本时学习,如果您有任何指点,将不胜感激。
编辑:
我处理的大多数数组元素都是数字,但有些包含字符串。我试图删除数组元素中的所有字符串。
例如
FD3459002912
*345*9002912$
【问题讨论】: