I have a pandas dataframe, df
:
c1 c2
0 10 100
1 11 110
2 12 120
How do I iterate over the rows of this dataframe? For every row, I want to access its elements (values in cells) by the name of the columns. For example:
for row in df.rows:
print(row['c1'], row['c2'])
I found a similar question, which suggests using either of these:
-
for date, row in df.T.iteritems():
-
for row in df.iterrows():
But I do not understand what the row
object is and how I can work with it.
DataFrame.iterrows
is a generator which yields both the index and row (as a Series):Obligatory disclaimer from the documentation
Other answers in this thread delve into greater depth on alternatives to iter* functions if you are interested to learn more.