1

I have 2 dataframe that are the same size.

I am not sure if "merge" is the right word here, but I would like to create a new dataframe with values that is minimum between the two.

df_1 ask bid A 0 2 B 0.643319 1.000000 C 0.722802 0.920498 df_2 ask bid A 1 0.643319 B 0.643319 0 C 0.722802 0.920498 df_3 ask bid A 0 0.643319 B 0.643319 0 C 0.722802 0.920498 

Thanks

2
  • Can you add an expected result. Requirement is not clear from the question Commented Feb 14, 2017 at 18:47
  • I added the expect result. Df3 Commented Feb 14, 2017 at 18:49

2 Answers 2

2

You could use DataFrame.where:

df_3 = df_1.where(df_1 < df_2, df_2) 

df_1.where returns the value from df_1 when the condition, df_1 < df_2 is True, and the corresponding value from df_2 when the condition is False.

Sign up to request clarification or add additional context in comments.

3 Comments

worked perfectly... Why are you guys so good at this? Thanks man.
90% of the problems are solved with 10% of the tools. So be happy every time you encounter something new -- there is a great chance you'll have an opportunity to use it again :)
@WeiZeng I noticed you've never accepted an answer before... you have the ability to accept whatever answer you find the most useful to your questions with the green check-mark to the left.
2

One option would be numpy.minimum.

>>> df1 bid ask 0 4 3 1 4 4 >>> df2 bid ask 0 9 2 1 5 1 >>> pd.DataFrame(np.minimum(df1, df2)) bid ask 0 4 2 1 4 1 

Comments