Excerpt
I have a pandas dataframe, df:
```plain text
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 be able to access its elements (values in cells) by the name of the columns. For example:
```plain text
for row in df.rows:
print(row['c1'], row['c2'])
```
I found a similar question which suggests using either of these:
```plain text
for date, row in df.T.iteritems():
```
```plain text
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):
```plain text
import pandas as pd
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
df = df.reset_index() # make sure indexes pair with number of rows
for index, row in df.iterrows():
print(row['c1'], row['c2'])
```
```plain text
10 100
11 110
12 120
```
## How to iterate over rows in a DataFrame in Pandas?
#
I have a pandas dataframe, df:
```plain text
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 be able to access its elements (values in cells) by the name of the columns. For example:
```plain text
for row in df.rows:
print(row['c1'], row['c2'])
```
I found a similar question which suggests using either of these:
```plain text
for date, row in df.T.iteritems():
```
```plain text
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):
```plain text
import pandas as pd
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
df = df.reset_index() # make sure indexes pair with number of rows
for index, row in df.iterrows():
print(row['c1'], row['c2'])
```
```plain text
10 100
11 110
12 120
```
## How to iterate over rows in a DataFrame in Pandas?
# Answer: DON'T!
Iteration in Pandas is an anti-pattern and is something you should only do when you have exhausted every other option. You should not use any function with "iter" in its name for more than a few thousand rows or you will have to get used to a lot of waiting.
Do you want to print a DataFrame? Use DataFrame.to_string().
Do you want to compute something? In that case, search for methods in this order (list modified from here):
1. Vectorization
2. List Comprehensions (vanilla for loop)
3. DataFrame.apply(): i) Reductions that can be performed in Cython, ii) Iteration in Python space
iterrows and itertuples (both receiving many votes in answers to this question) should be used in very rare circumstances, such as generating row objects/nametuples for sequential processing, which is really the only thing these functions are useful for.
Appeal to Authority
The documentation page on iteration has a huge red warning box that says:
Iterating through pandas objects is generally slow. In many cases, iterating manually over the rows is not needed [...].
- It's actually a little more complicated than "don't". df.iterrows() is the correct answer to this question, but "vectorize your ops" is the better one. I will concede that there are circumstances where iteration cannot be avoided (for example, some operations where the result depends on the value computed for the previous row). However, it takes some familiarity with the library to know when. If you're not sure whether you need an iterative solution, you probably don't. PS: To know more about my rationale for writing this answer, skip to the very bottom.