Rafael Rodrigues Santos Rafael Rodrigues Santos - 1 month ago 5
Python Question

Slow performance - Python (code)

I have the following code below which works quite well for what I want to do:
However, it takes 40 minutes to complete for 64000 rows.
The sricpt creates a new column called PN 3D which takes only the first 3 numbers from the column "Part No.

for pn in ro['Part No.']:
ro['PN 3D'] = ro['Part No.'].apply(lambda pn: str(pn)[:3])


There is any other way to speed up my program?
Thank in advance

Answer

You don't need the loop:

ro['PN 3D'] = ro['Part No.'].apply(lambda pn: str(pn)[:3])
Comments