I have this Dataframe:
V1 V2
1 1
0 0
0 0
0 0
1 0
1 1
0 1
I need to compare each V1 and V2 row value with an variable called comienzo, and to set a third column named Label following this function:
def labeling(DataFrame):
comienzo=0 #flag to indicate event started (1= started, 0= no started)
for n in range(len(DataFrame)):
if ((DataFrame.iloc[n]['V1']==1 & DataFrame.iloc[n]['V2']==1) & comienzo == 0 ) :
comienzo=1
DataFrame['Label']='Start'
elif ((DataFrame.iloc[n]['V1']==0 & DataFrame.iloc[n]['V2']==0) & comienzo == 1 ) :
comienzo=0
DataFrame['Label']='End'
return DataFrame
I want to do this pandorable using Dataframe.apply
. So, I tried this:
def labeling(x, comienzo):
if ((x['V1']==1 & x['V2']==1) & comienzo == 0 ) :
comienzo=1
Label='Start'
elif ((x['V1']==0 & x['V2']==0) & comienzo == 1 ) :
comienzo=0
Label='End'
return Label
comienzo=0 #I initialize 'comienzo' var to 0
DataFrame['Label']=DataFrame.apply(labmda x: labeling(x,comienzo),axis=1)
This work but values are incorrect, I think that .apply
doesn't take into account variable comienzo.
Is it possible make this code pandorable?
I want this output:
comienzo=0
V1 V2
1 1 Start comienzo=1
0 1 NaN
0 0 End comienzo=0
0 0 NaN
1 0 NaN
1 1 Start comienzo=1
1 1 NaN
0 1 NaN