Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way in which I can sort every pair of columns with the same name in a df on Python? Pandas related

Let’s say I have the following df:

       0       1     2      3       4     5       0                1        2         3     4        5
0  Fondo  Cuerpo  Ojos  Color  Pinzas  Puas  Oceano  Cuerpo cangrejo  Antenas  Amarillo  None     None
1  Fondo  Cuerpo  Ojos  Color  Pinzas  Puas  Oceano  Cuerpo cangrejo  Antenas  Amarillo  None    Arena
2  Fondo  Cuerpo  Ojos  Color  Pinzas  Puas  Oceano  Cuerpo cangrejo  Antenas  Amarillo  None   Marron
3  Fondo  Cuerpo  Ojos  Color  Pinzas  Puas  Oceano  Cuerpo cangrejo  Antenas  Amarillo  None  Purpura
4  Fondo  Cuerpo  Ojos  Color  Pinzas  Puas  Oceano  Cuerpo cangrejo  Antenas  Amarillo  None    Verde

How can I make the columns with the same name get sorted like this?

       0      0      1               1    2       2     3        3      4    4    5     5         
0  Fondo Oceano Cuerpo Cuerpo cangrejo Ojos Antenas Color Amarillo Pinzas None Puas None            
1  Fondo Oceano Cuerpo Cuerpo cangrejo Ojos Antenas Color Amarillo Pinzas None Puas Arena     
2  Fondo Oceano Cuerpo Cuerpo cangrejo Ojos Antenas Color Amarillo Pinzas None Puas Marron    
3  Fondo Oceano Cuerpo Cuerpo cangrejo Ojos Antenas Color Amarillo Pinzas None Puas Purpura    
4  Fondo Oceano Cuerpo Cuerpo cangrejo Ojos Antenas Color Amarillo Pinzas None Puas Verde    

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use sort_index with axis=1:

In [460]: res.sort_index(1, inplace=True)

In [461]: res
Out[461]: 
       0       0       1                1     2        2      3         3       4     4     5        5
0  Fondo  Oceano  Cuerpo  Cuerpo cangrejo  Ojos  Antenas  Color  Amarillo  Pinzas  None  Puas     None
1  Fondo  Oceano  Cuerpo  Cuerpo cangrejo  Ojos  Antenas  Color  Amarillo  Pinzas  None  Puas    Arena
2  Fondo  Oceano  Cuerpo  Cuerpo cangrejo  Ojos  Antenas  Color  Amarillo  Pinzas  None  Puas   Marron
3  Fondo  Oceano  Cuerpo  Cuerpo cangrejo  Ojos  Antenas  Color  Amarillo  Pinzas  None  Puas  Purpura
4  Fondo  Oceano  Cuerpo  Cuerpo cangrejo  Ojos  Antenas  Color  Amarillo  Pinzas  None  Puas    Verde
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading