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

Time:11-24

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:

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!

CodePudding user response:

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
  • Related