i have the following dataset:
JavaScript
x
21
21
1
A B C D E F
2
154.6175111 148.0112337 155.7859835 1 1 x
3
255 253.960131 242.5382584 1 1 x
4
251.9665958 235.1105659 185.9121703 1 1 x
5
137.9974994 225.3985177 254.4420772 1 1 x
6
85.74722877 116.7060415 158.4608395 1 1 x
7
123.6969939 140.0524405 132.6798037 1 1 x
8
133.3251695 80.08976196 38.81201612 1 1 y
9
118.0718812 243.5927927 255 1 1 y
10
189.5557302 139.9046713 91.90519519 1 1 y
11
172.3117291 188.000268 129.8155501 1 1 y
12
48.07634611 21.9183119 25.99669279 1 1 y
13
23.40525987 8.395857933 25.62371342 1 1 y
14
228.753009 164.0697727 172.6624107 1 1 z
15
203.3405006 173.9368303 189.8103708 1 1 z
16
184.9801932 117.1591341 87.94739034 1 1 z
17
29.55251224 46.03945452 70.7433477 1 1 z
18
143.6159623 120.6170926 155.0736604 1 1 z
19
142.5421179 128.8916843 169.6013111 1 1 z
20
21
i want to combine x y z into another dataframe like this:
JavaScript
1
5
1
A B C D E F
2
154.6175111 148.0112337 155.7859835 1 1 x ->first x value
3
133.3251695 80.08976196 38.81201612 1 1 y ->first y value
4
228.753009 164.0697727 172.6624107 1 1 z ->first z value
5
and i want these dataframes for each x y z value like first, second third and so on.
how can i select and combine them?
desired output:
JavaScript
1
30
30
1
A B C D E F
2
154.6175111 148.0112337 155.7859835 1 1 x
3
133.3251695 80.08976196 38.81201612 1 1 y
4
228.753009 164.0697727 172.6624107 1 1 z
5
6
A B C D E F
7
255 253.960131 242.5382584 1 1 x
8
118.0718812 243.5927927 255 1 1 y
9
203.3405006 173.9368303 189.8103708 1 1 z
10
11
A B C D E F
12
251.9665958 235.1105659 185.9121703 1 1 x
13
189.5557302 139.9046713 91.90519519 1 1 y
14
184.9801932 117.1591341 87.94739034 1 1 z
15
16
A B C D E F
17
137.9974994 225.3985177 254.4420772 1 1 x
18
172.3117291 188.000268 129.8155501 1 1 y
19
29.55251224 46.03945452 70.7433477 1 1 z
20
21
A B C D E F
22
85.74722877 116.7060415 158.4608395 1 1 x
23
48.07634611 21.9183119 25.99669279 1 1 y
24
143.6159623 120.6170926 155.0736604 1 1 z
25
26
A B C D E F
27
123.6969939 140.0524405 132.6798037 1 1 x
28
23.40525987 8.395857933 25.62371342 1 1 y
29
142.5421179 128.8916843 169.6013111 1 1 z
30
Advertisement
Answer
Use GroupBy.cumcount
for counter and then loop by another groupby object:
JavaScript
1
5
1
g = df.groupby('F').cumcount()
2
3
for i, g in df.groupby(g):
4
print (g)
5