I have a csv file in which such a column is calculated:
Combined
0
0
1
0
0
0
2
0
0
0
3
0
0
0
0
-1
0
0
0
0
-2
0
0
0
0
-3
0
0
0
1
I would like to change the values in it so that when, for example, the value -1 appears, all zeros up to the value -2 turn into -1, which would turn out something like this
Combined
0
0
1
1
1
1
2
2
2
2
3
3
3
3
3
-1
-1
-1
-1
-1
-2
-2
-2
-2
-2
-3
-3
-3
-3
1
I combine it into class functions in this way
cols = ['Answers', 'step', 'step2']
df['combined'] = df[cols].apply(lambda row: ''.join(row.values.astype(str)), axis=1)
df['combined'] = df['combined'].str.replace('0.000', 'O',regex=True)
df['combined'] = df['combined'].map(lambda x: x.lstrip('0.0').rstrip('.000'))
df['combined'] = df['combined'].str.replace('O', '0')
For the sake of explanation ! At the beginning of this csv there is always a pair of zeros, but after them either -1 or 1 always comes and then it always goes depending on the first number either -2 or 2 and the three comes exactly the same depending on the second number either -3 or 3 And after its cycle repeats and also depending on the three, either -1 or 1.