Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create dataframe based on conditionals from other dataframes AND past (t-1) values

I would like to create a new dataframe depending on the values from two other dataframes (same shape) and, also, comparing t and t-1 (immediately before) values from one of these dataframes.

I have two dataframes: P and PR. The logic for the third one would be: If (P_t >= 30.6 & PR_t <PR_t-1) then 1; else 0. So for example:

P = pd.DataFrame({'col1': [26.7,24.7,26.1,26.4,24.5], 'col2': [30.8,30.8,30.7,30.8,29.8], 'col3': [30.8,30.7,30.5,30.6,30.0]})
PR = pd.DataFrame({'col1': [79.8,73.6,81.1,79.4,75.7], 'col2': [74.1,74.1,77.0,74.7,74.1], 'col3': [74.0,74.0,76.4,74.3,74.8]})

Would give me a resulting dataframe like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pd.DataFrame({'col1': [0,0,0,0,0], 'col2': [0,1,0,1,0], 'col3': [0,1,0,0,0]})

Any help is much appreciated!

>Solution :

You can use:

(P.ge(30.6) & PR.diff().lt(0)).astype(int)

Output:

   col1  col2  col3
0     0     0     0
1     0     0     0
2     0     0     0
3     0     1     1
4     0     0     0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading