Skip to content
Advertisement

How to combine rows into seperate dataframe python pandas

i have the following dataset:

A           B           C           D   E   F
154.6175111 148.0112337 155.7859835 1   1   x
255 253.960131  242.5382584         1   1   x
251.9665958 235.1105659 185.9121703 1   1   x
137.9974994 225.3985177 254.4420772 1   1   x
85.74722877 116.7060415 158.4608395 1   1   x
123.6969939 140.0524405 132.6798037 1   1   x
133.3251695 80.08976196 38.81201612 1   1   y
118.0718812 243.5927927 255         1   1   y
189.5557302 139.9046713 91.90519519 1   1   y
172.3117291 188.000268  129.8155501 1   1   y
48.07634611 21.9183119  25.99669279 1   1   y
23.40525987 8.395857933 25.62371342 1   1   y
228.753009  164.0697727 172.6624107 1   1   z
203.3405006 173.9368303 189.8103708 1   1   z
184.9801932 117.1591341 87.94739034 1   1   z
29.55251224 46.03945452 70.7433477  1   1   z
143.6159623 120.6170926 155.0736604 1   1   z
142.5421179 128.8916843 169.6013111 1   1   z

i want to combine x y z into another dataframe like this:

A           B           C           D   E   F
154.6175111 148.0112337 155.7859835 1   1   x  ->first x value
133.3251695 80.08976196 38.81201612 1   1   y  ->first y value
228.753009  164.0697727 172.6624107 1   1   z  ->first z value

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:

A           B           C           D   E   F
154.6175111 148.0112337 155.7859835 1   1   x
133.3251695 80.08976196 38.81201612 1   1   y
228.753009  164.0697727 172.6624107 1   1   z

A           B           C           D   E   F
255 253.960131  242.5382584         1   1   x
118.0718812 243.5927927 255         1   1   y
203.3405006 173.9368303 189.8103708 1   1   z

A           B           C           D   E   F
251.9665958 235.1105659 185.9121703 1   1   x
189.5557302 139.9046713 91.90519519 1   1   y
184.9801932 117.1591341 87.94739034 1   1   z

A           B           C           D   E   F
137.9974994 225.3985177 254.4420772 1   1   x
172.3117291 188.000268  129.8155501 1   1   y
29.55251224 46.03945452 70.7433477  1   1   z

A           B           C           D   E   F
85.74722877 116.7060415 158.4608395 1   1   x
48.07634611 21.9183119  25.99669279 1   1   y
143.6159623 120.6170926 155.0736604 1   1   z

A           B           C           D   E   F
123.6969939 140.0524405 132.6798037 1   1   x
23.40525987 8.395857933 25.62371342 1   1   y
142.5421179 128.8916843 169.6013111 1   1   z

Advertisement

Answer

Use GroupBy.cumcount for counter and then loop by another groupby object:

g = df.groupby('F').cumcount()

for i, g in df.groupby(g):
    print (g)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement