Reputation: 185
I currently have the following dataframe
and need to create a new binary data frame which applies the following logic:
-If value in any column is >=1.5 and the preceeding value is <1.5, then show 1.
-Continue to show 1 unless the value drops <=0, then put 0.
So applying this to the above should yield:
Any idea how to do this most efficiently?
Upvotes: 0
Views: 149
Reputation: 516
Here is the approach I think should work. First create new columns for each of the existing columns which will contain the existing column shifted upwards by one column. ie if A is 0 1 1.5 create A1 to 1 1.5 nan U can use pandas shift to do that. do it for all columns Then, if df is the name of your dataFrame
not_found_zero=1
for row in df.iterrows():
if(not_found_zero):
if row['A']<=0:
row['A']=0
flag=0
else:
row['A'] =(row['A']>=1.5) & (row['A1']>=1.5))
else:
row['A']=0
Do this for all columns,and remember after u shift ,last column of A1 will be nan. Delete the temperory rows after completion.
Upvotes: 1