I am very new to python. I encountered this task and was on it for a while without a clue. Any suggestions can help! Thanks a lot.
I have a data frame like this:
import pandas as pd
data = {'A': ['Emo/3', 'Emo/4', 'Emo/1','Emo/3', '','Emo/3', 'Emo/4', 'Emo/1','Emo/3', '', 'Neu/5', 'Neu/2','Neu/5', 'Neu/2'],
'Pos': ["repeat3", "repeat3", "repeat3", "repeat3", '',"repeat1", "repeat1", "repeat1", "repeat1", '', "repeat2", "repeat2","repeat2", "repeat2"],
}
df = pd.DataFrame(data)
df
A Pos
0 Emo/3 repeat3
1 Emo/4 repeat3
2 Emo/1 repeat3
3 Emo/3 repeat3
4
5 Emo/3 repeat1
6 Emo/4 repeat1
7 Emo/1 repeat1
8 Emo/3 repeat1
9
10 Neu/5 repeat2
11 Neu/2 repeat2
12 Neu/5 repeat2
13 Neu/2 repeat2
I want a output like this:
A Pos B
0 Emo/3 repeat3 0
1 Emo/4 repeat3 0
2 Emo/1 repeat3 0
3 Emo/3 repeat3 0
4
5 Emo/3 repeat1 1
6 Emo/4 repeat1 2
7 Emo/1 repeat1 3
8 Emo/3 repeat1 4
9
10 Neu/5 repeat2 4
11 Neu/2 repeat2 2
12 Neu/5 repeat2 3
13 Neu/2 repeat2 1
The first four position of the column"B" is always 0. Then the other positions in the column "B" are based on the value in column"pos". if the row in column"pos" equal "repeat 1" then the column "B" at that four positions will be: 1, 2, 3, 4. If the row in column"pos" equal "repeat 2", then the column "B" at the four positions will be: 4, 3, 2, 1.
The values in Pos are always arranged as every four rows with the same value and the fifth row will be empty.
Thanks a lot!