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

Create cols based on rows pandas

I have a df that can be seen below where I have the column ‘sex’ which indicates female/male and a column count which is the number of females/males present.

region organismo_nombre sexo conteo
Región Metropolitana de Santiago subsecretaria de derechos humanos F 56
Región Metropolitana de Santiago subsecretaria de derechos humanos M 35
Región Metropolitana de Santiago subsecretaria de evaluación social F 121
Región Metropolitana de Santiago subsecretaria de evaluación social M 106

I want instead of having two rows, to have only one with column ‘F’ indicating the number of women and column ‘M’ indicating the number of men.
I did some tests with df.pivot but I still had two rows left. How could I achieve the df below?

region organismo_nombre F M
Región Metropolitana de Santiago subsecretaria de derechos humanos 56 35
Región Metropolitana de Santiago subsecretaria de evaluación social 121 106

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 pd.pivot

df.pivot(index=['region','organismo_nombre'], columns='sexo').reset_index()

    region                              organismo_nombre                      conteo
sexo                                                                          F     M
0   Región Metropolitana de Santiago    subsecretaria de derechos humanos    56     35
1   Región Metropolitana de Santiago    subsecretaria de evaluación social  121     106
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