corcholatacolormarengo
corcholatacolormarengo

Reputation: 411

How to iterate row by row in a pandas dataframe and look for a value in its columns

I must read each row of an excel file and preform calculations based on the contents of each row. Each row is divided in columns, my problem is that I cannot find a way to access the contents of those columns.

I'm reading the rows with:

for i in df.index,:
    print(df.loc[i])

Which works well, but when I try to access, say, the 4h column with this type of indexing I get an error:

for i in df.index,:
    print(df.loc[i][3])

I'm pretty sure I'm approaching the indexing issue in the wrong way, but I cannot figure put how to solve it.

Upvotes: 1

Views: 13339

Answers (1)

Lucas Araújo
Lucas Araújo

Reputation: 429

You can use iterrows(), like in the following code:

for index, row in dataFrame.iterrows():
  print(row)

But this is not the most efficient way to iterate over a panda DataFrame, more info at this post.

Upvotes: 5

Related Questions