Skip to content
Advertisement

How to efficiently join a small table to a large one on python

I have two tables similar to these:

JavaScript

My goal is to multiply df['x'] by df['y'] based on id. My current solution works, but it seems to me that there should be a more efficient/elegant way of doing this.

This is my code:

JavaScript

Advertisement

Answer

You can use map to recover the multiplication factor from df_2, then mul to the x column:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement