I am new to the python . Now, Here I have a dataframe which looks like this .
DocumentId offset feature
0 0 2000
0 7 2000
0 16 0
0 27 0
0 36 0
0 40 0
0 46 0
0 57 0
0 63 0
0 78 0
0 88 0
0 91 0
0 103 2200
1 109 0
1 113 2200
1 126 2200
1 131 2200
1 137 2200
1 142 0
1 152 0
1 157 200
1 159 200
1 161 200
1 167 0
1 170 200
Now, Here In this data-frame, I have a column called feature
. It has some values, where it also has a value 0
and others are positive values
.
Now Here, whenever the positive value gets changed to the zero value then we have to take the previous three values of that. So,we are assuming that it will always start with the positive value .
So from this data set ,
I will get the result for first will be
offset1 feature offset2 feature offset3 feature
- - 0 2000 7 2000
for the `(-)` as it does not have the third value.
Now, it goes on checking the values, whenever the value positive to zero becomes I have to take the previous three of that.
So, Here ,
after 2200 which is at offset 103 value becomes the 0 which is at 109
Now, I have to take the previous three values of the 109 offset
so the data becomes
offset1 feature offset2 feature offset3 feature
- - 0 2000 7 2000
103 2200 91 0 88 0
After this the value from positive to 0 becomes at offset 142
So, I have to take previous three .
offset1 feature offset2 feature offset3 feature
- - 0 2000 7 2000
103 2200 91 0 88 0
137 2200 131 2200 126 2200
161 200 159 200 157 200
offset4 feature offset5 feature offset6 feature
103 2200 109 0 113 2200
113 2200 126 2200 131 2200
157 200 159 200 161 200
So, Here If we see in the dataframe which starts with a positive number now, If we go down then at
the place `103` the 0 becomes a positive value which is the 2200
Now ,Here I am taking the next two values of that offset.which are 109 and 113 and also the current number value which will be 103
SO, the result will be
offset4 feature offset5 feature offset6 feature
103 2200 109 0 113 2200
Now, after this If I go down, then immediately at 113 the previous value was 0 that gets changed to 2200
SO, I will take the next two values after the 113 that are 126, 131 so the next becomes
offset4 feature4 offset5 feature5 offset5 feature5
113 2200 126 2200 131 2200
Now again down then at 157 it gets changed to the 200 as so it will be same
.
for this next values I am taking the next two values and itself so it become 3 values. Here whenever the value gets changed from 0 to a positive
In this way I am taking the next values.
Is there any way though which I can achieve this result ? thanks.
what I tried is
zero_indexes = list(input_with[input_csv['RFC_PREDICTEDFEATURE'] == 0].index)
df2 = pd.DataFrame()
for each_zero_index in zero_indexes:
value = input_with['feature'].loc[each_zero_index - 1: each_zero_index].values[0]
if value != 0 :
df1 = input_with.loc[each_zero_index - 3: each_zero_index]
df2 = df2.append(df1)