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

what is the syntax to use groupby by column index rather than column name?

here i have a code snip that creates a groupby object with the count of duplicates.

gbo = data2.groupby("1").count()
display(gbo)

this works perfectly because the column names are indexes but will not work if the columns do not have a numeric index. i want to call the column by index number for the groupby function and need the correct simple syntax. this is the output of the above code and has the correct result.

    0   2   3   4   5   
1                                               
Alph    7   6   5   4   
Beta    351 350 337 303 
Gamma   234 233 230 226 
Delt    2   1   0   0   

the issue is that if the columns were designated as Col1, Col2, Col3.. ect,. how do i access Col1 as column index0 or column 2 as column index1. i cant seem to find the proper syntax

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 :

You can specify the index as such, indicating the level without quotations will specify the index.

gbo = data2.groupby(level=1).count()
display(gbo)
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