Why is the apply() so slow?

68 views
Skip to first unread message

Junwei Pan

unread,
Jun 9, 2016, 11:18:12 PM6/9/16
to PyData
Say that we want to convert one column of the dataframe from float type to int type, we can do so in two ways:

1. df['y'] = df['x'].astype(int)
2. df['y'] = df.apply(lambda x : int(x['x'] ), axis=1)

Why is the method 1 mush faster than method 2?

Thx

Pietro Battiston

unread,
Jun 10, 2016, 3:50:41 AM6/10/16
to pyd...@googlegroups.com
Because in the second you are iterating in Python, which is
inefficient.

http://bfy.tw/6CMk

Pietro

Junwei Pan

unread,
Jun 11, 2016, 12:28:35 PM6/11/16
to PyData
OK, I see. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages