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

How to make two dataframes one with values and another with boolean into one dataframe in python?

For example, I have two dataframes like:

dataframe1 would be

            A     B     C     D     E
values1  0.25  0.33  0.12  0.22  0.08
values2  0.20  0.50  0.89  0.65  0.75

and dataframe2 would be

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

              A     B     C      D     E
boolean1   True False  True  False  True
boolean2  False False  True   True  True

and want a result with one dataframe:

      A  B     C     D     E
1  0.25  0  0.12     0  0.08  
2     0  0  0.89  0.65  0.78

So if it is True in dataframe2 just bring the value from the dataframe1 and if it is False then replace with 0. How can I do this?

>Solution :

You can use

df1 = df1.where(df2.values, 0)
# or
df1 = df1.mask(~df2.values, 0)
print(df1)

            A    B     C     D     E
values1  0.25  0.0  0.12  0.00  0.08
values2  0.00  0.0  0.89  0.65  0.75
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