Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient alternative in 04_ApplyStudents_Alcohol_Consumption #78

Open
rahimnathwani opened this issue Mar 6, 2019 · 2 comments
Open

Comments

@rahimnathwani
Copy link

In step 10, we want to multiply all numerical values by 10.

The provided solution is:
df.applymap(times10).head(10)

But this is very slow, because it runs a regular python function on every element in the dataframe.

Better is to test each column's type, and then use pandas built in multiplication on the whole column:

for colname, coltype in df.dtypes.to_dict().items():
    if coltype.name in ['int64']:
        df[colname] = df[colname] * 10

I used %%timeit to test the two solutions. On this small dataset, my solution is 5x as fast (1.1ms vs 5.8ms). The difference would get larger with a larger dataset.

@pcarlitz
Copy link

pcarlitz commented Jun 11, 2019

what if it's not an int64 though? This might work better.

newdf = df.select_dtypes(include=[np.number])
for column in newdf.columns:
    newdf[column] = newdf[column] * 10 

@guipsamora
Copy link
Owner

@pcarlitz have you measured the performance? I am in favor of the fastest solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants