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

If a dataframe contains a value in a column, how to perform a calculation in another column? – Pandas/ Python

Original Dataframe:

A B C
123 1500 0

Output:

A B C
123 1500 1.2

Logic needed: If column A contains 123, then for column C, take the value of B and multiply it with 0.0008, all the other values in column C should not be altered.

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

>Solution :

You could check if column "A" values are 123 or not and use mask on "C" to replace values there:

df['C'] = df['C'].mask(df['A']==123, df['B']*0.0008)

Output:

     A     B    C
0  123  1500  1.2
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