Home > database >  pandas update only true values with another dataframe
pandas update only true values with another dataframe

Time:08-03

I have the following two tables:

df1 = pd.DataFrame(data={'val1': [True, False, False, True], 
                         'val2': [False, True, False, True], 
                         'val3': [True, True, False, True]},
                   index=pd.Series([1, 2, 3, 4], name='index'))
index val1 val2 val3
1 True False True
2 False True True
3 False False False
4 True True True
df2 = pd.DataFrame(data={'val1': [1, 3, 3, 0], 
                         'val2': [5, 2, 2, 4], 
                         'val3': [5, 5, 3, 0]},
                   index=pd.Series([1, 2, 3, 4], name='index'))
index val1 val2 val3
1 1 5 5
2 3 2 5
3 3 2 3
4 0 4 0

How do I only update the values in df1 that are True to get the following?

index val1 val2 val3
1 1 False 5
2 False 2 5
3 False False False
4 0 4 0

I have tried df1.update(df2), but it override the False values in df1 as well.

CodePudding user response:

Use where to replace values in df2.

# replace values
df2.where(df1, False).reset_index()

result

CodePudding user response:

df1.replace(True, pd.NA, inplace=True)
df1.update(df2, overwrite=False)

Output:

        val1   val2   val3
index                     
1          1  False      5
2      False      2      5
3      False  False  False
4          0      4      0
  • Related