Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas math operation with where condition

I want to do math on a condition in a dataframe, but I couldn’t.

my code:

open_position=df.loc[df['sale_price'].isna(), 'purchase_price'*'amount'].sum()

I want to do. Multiply df['purchase_price'] and df['amount'] in rows where df['sales_price'] is NaN and get the sum of all these.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it gives the following error.

TypeError: can't multiply sequence by non-int of type 'str'

Can you help with this?

>Solution :

Try this:

df.loc[df['sale_price'].isna(), 'purchase_price'].mul(df['amount']).sum()

or

df['product'] = df['purchase_price'] * df['amount']
df.loc[df['sale_price'].isna(), 'product'].sum()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading