python - Pandas elementwise conditional operation over multiple dataframes -


i'd create dataframe based on combination of conditional operations (elementwise) on multiple dataframes same structure (same index, same columns).

here i've created 3 dataframes similar strutures.

df1 = pd.dataframe(np.random.rand(5,3), columns=['a','b','c'],index = pd.date_range(start='2000.01.01', periods=5, freq='d')) df2 = pd.dataframe(np.random.rand(5,3), columns=['a','b','c'],index = pd.date_range(start='2000.01.01', periods=5, freq='d')) df3 = pd.dataframe(np.random.rand(5,3), columns=['a','b','c'],index = pd.date_range(start='2000.01.01', periods=5, freq='d'))  df1                            b         c 2000-01-01  0.457567  0.157506  0.185594 2000-01-02  0.709991  0.486635  0.839173 2000-01-03  0.503184  0.640214  0.895055 2000-01-04  0.940231  0.591708  0.019716 2000-01-05  0.246132  0.596872  0.437000  df2                            b         c 2000-01-01  0.722588  0.696100  0.176172 2000-01-02  0.275177  0.162525  0.347674 2000-01-03  0.248735  0.887237  0.175126 2000-01-04  0.444136  0.337881  0.830616 2000-01-05  0.526365  0.803296  0.574811  df3                             b         c 2000-01-01  0.392965  0.107987  0.139133 2000-01-02  0.751523  0.658844  0.174854 2000-01-03  0.509276  0.380294  0.406262 2000-01-04  0.669822  0.079491  0.233737 2000-01-05  0.659077  0.094545  0.826730 

here goes pseudocode:

df4 = if (df1 > 0.5 , df2 <0.3 , df3 > 0.6, 1, 0) 

what simple , efficient code this?

pandas

(df1.gt(.5) & df2.lt(.3) & df3.gt(.6)).astype(int)               b  c 2000-01-01  0  0  0 2000-01-02  1  0  0 2000-01-03  0  0  0 2000-01-04  0  0  0 2000-01-05  0  0  0 

with numpy v1

pd.dataframe(     (         (df1.values > .5) &         (df2.values < .3) &         (df3.values > .6)     ).astype(int),     df1.index, df1.columns )               b  c 2000-01-01  0  0  0 2000-01-02  1  0  0 2000-01-03  0  0  0 2000-01-04  0  0  0 2000-01-05  0  0  0 

with numpy v2

pd.dataframe(     np.where(         (df1.values > .5) &         (df2.values < .3) &         (df3.values > .6), 1, 0     ),     df1.index, df1.columns )               b  c 2000-01-01  0  0  0 2000-01-02  1  0  0 2000-01-03  0  0  0 2000-01-04  0  0  0 2000-01-05  0  0  0 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -