Let's suppose that I have the following in python
and pandas
:
Names Values
0 A 6
1 B 8
2 C 3
3 D 5
4 E 1
5 F 3
6 D 9
7 E 6
8 F 4
9 G 3
10 D 1
11 E 5
12 F 6
and I want to transform this to the following:
Name_1 Values_1 Name_2 Values_2 Name_3 Values_3
0 D 5 D 9 D 1
1 E 1 E 6 E 5
2 F 3 F 4 F 6
3 NA NA G 3 NA NA
Basically what I want to do is to split the original pairs of columns in chunks of data which start with the row where Names
is D
and ends with the row exactly before the next Names
is D
starts.
What is the most efficient way to do this?