Issue
I have 2 dataframes: df1 and df2
df1 = pd.DataFrame()
df1['Cat'] = [0,1,2]
df1['Mouse'] = [12,13,17]
df1['Dog'] = [18,22,89]
df1
Cat Mouse Dog
0 0 12 18
1 1 13 22
2 2 17 89
df2 = pd.DataFrame()
df2['Cat'] = [0.94, 0.86, 0.04]
df2['Mouse'] = [1,0.12,0.13]
df2['Dog'] = [0.03,0.025,0.17]
df2
Cat Mouse Dog
0 0.94 1.00 0.030
1 0.86 0.12 0.025
2 0.04 0.13 0.170
I want to replace the values in df1 with NaN only if the df2 value is < 0.05. How do I code for this condition?
My desired output would be as follows:
Cat Mouse Dog
0 0.0 12 NaN
1 1.0 13 NaN
2 NaN 17 89.0
Thanks!
EDIT - Thanks for all the suggestions, is there a way for me to amend the code if my columns in df1 and df2 were not in the same order? I.e. of df1 columns were Cat, dog, mouse and df2 was dog, mouse, cat
Solution
You can achieve it by doing:
>>> df1.where(df2 > 0.05)
Cat Mouse Dog
0 0.0 12 NaN
1 1.0 13 NaN
2 NaN 17 89.0
Answered By - Mabel Villalba
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.