【发布时间】:2021-05-04 19:49:32
【问题描述】:
我有这个基本数据框:
dur type src dst
0 0 new 543 1
1 0 new 21 1
2 1 old 4828 2
3 0 new 321 1
...
(total 450000 rows)
我的目标是根据值将 src 中的值替换为 0、1 或 2。我在下面创建了一个 for 循环/if else:
for i in df['src']:
if i <= 1000:
df['src'].replace(to_replace = [i], value = [1], inplace = True)
elif i <= 2500:
df['src'].replace(to_replace = [i], value = [2], inplace = True)
elif i <= 5000:
df['src'].replace(to_replace = [i], value = [3], inplace = True)
else:
print('End!')
上述工作按预期工作,但尝试用 450000 行替换整个数据框非常慢(这需要 30 多分钟!)。
有没有更 Pythonic 的方式来加速这个算法?
【问题讨论】:
-
使用嵌套的
where或cut
标签: python python-3.x pandas for-loop if-statement