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

How to browse pandas dataframe using row number eand column number

I am trying to browse my DF doing :

for row in range(len(DF)):
    for col in range(len(DF.columns)):
        print(DF[row][col])

but doing so throw :

KeyError: 0

I am wondering, how to browse a whole dataframe (including col name and excluding index) using only col number and row number.

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 :

If need select values by indices use DataFrame.iat/ DataFrame.iloc:

for row in range(len(DF)):
    for col in range(len(DF.columns)):
        print(DF.iat[row, col])
        #print(DF.iloc[row, col])

Better selecting by values of index, columns, it is same ouput if not duplicated index/columns values use DataFrame.at/DataFrame.loc:

for row in DF.index:
    for col in DF.columns:
        print(DF.at[row, col])
        #print(DF.loc[row, col])

BUT if possible use some vectorized solution the best is avoid looping.

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