Pandas - Compare Two Dataframes And Replace Values Matching Condition
I have two pandas dataframes(df1 and df2) with the exact same number of columns and rows. (colum and index names are the same as well) The values in these two dataframes may or may
Solution 1:
If memory is not a concern, I would create a third DataFrame of random numbers, and make a substitution using the difference as a mask.
For instance, something like
randoms = pd.DataFrame(
np.random.randn(*df1.values.shape),
index=df1.index,
columns=df1.columns
)
df1[df2 >= df1] = randoms[df2 >= df1]
Post a Comment for "Pandas - Compare Two Dataframes And Replace Values Matching Condition"