Skip to content
Advertisement

Pandas apply function to each row by calculating multiple columns

I have been stacked by an easy question, and my question title might be inappropriate.

JavaScript

I want to calculate (df.amount * df.con)/df.groupby('name').agg({'amount':'sum'}).reset_index().loc(df.name==i).amount) (Sorry, this line will return error, but what I want is to calculate total concentration (under each name) based on each ingredient amount and ingredient con.

Here is my code:

JavaScript

output:

JavaScript

Any short-cut for this calculation?

Thanks ahead.

Advertisement

Answer

IIUC, you can use:

JavaScript

output:

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