How To Iterate Over Rows in a DataFrame in Pandas Python

Are you using pandas in Python and want to iterate over rows or columns? This article will show you how to iterate over rows in a dataframe in Pandas using Python.

Suppose you have data with some values consisting of keys and values. And you want to iterate over that and print it out in form of rows and columns.

And after this, you want to iterate over that data either through rows or through columns. This iteration can be achieved using the iterrows() method present in the Pandas library.

1. Using DataFrame.iterrows To Iterate Overs rows in a DataFrame in Pandas

Once you have organized your data using the DataFrame[1] in the Pandas then DataFrame class consists of the method called iterrows that lets you iterate over the rows for the data that you have provided.

Let us see in the below example code the usage of the iterrows method for Pandas in Python.

#importing Pandas
import pandas as pd

#Initializing the Data
data = {'Column1':[1,2,3,4,5], 'Column2':[6,7,8,9,10]}

dataFrame = pd.DataFrame(data)

#Printing the Row and Columns
#print(dataFrame)

#Now Lets Iterate over Rows 
for rowIndex, row in dataFrame.iterrows():
    print(row['Column1'], row['Column2'])

Output:

1 6
2 7 
3 8 
4 9 
5 10

As you can see using the iterrows function I was able to print the rows of the given data. But if you are going to use a large amount of data for iteration then this function will be really slow.

Also, one of the other issue this method has is iterrows does not store the data types and this also makes the execution time for large data types a lot slow. Hence to improve the execution and preserve the data types you should use the below method.

2. Using itertuples to Iterate Over Rows In A DataFrame in Pandas

If you want to increase the efficiency of your program and need the best practice to be used then you should use the itertuples method that store the data types and also is faster than the iterrows method.

Let us see in the below example code the usage of itertuples in DataFrame in Pandas using Python.

#importing Pandas
import pandas as pd

#Initializing the Data
data = {'Column1':[1,2,3,4,5], 'Column2':[6,7,8,9,10]}

dataFrame = pd.DataFrame(data)

#Printing the Row and Columns
#print(dataFrame)

#Now Lets Iterate over Rows using itertuples
for tupleRow in dataFrame.itertuples(index= True):
    print(tupleRow.Column1, tupleRow.Column2)

Output:

1 6
2 7 
3 8 
4 9 
5 10

But there are also some issues with itertuples function and it is suggested not to be used if you have a large amount of data.

If any of the column names are invalid Python identifiers, are repeated, or begin with an underscore, the column names will be renamed to positional names. Tuples are returned when the number of columns in the table is greater than 255.

Hence for large data, you should use the itertuples or if you know how to do vectorization of DataFrame, then you can do that in order to increase the efficiency of the code.

How To Iterate Over Rows in a DataFrame in Pandas Python

Wrap Up

I hope you got your answer related to how to iterate over rows in a DataFrame in Pandas using Python. I have listed the two very important methods that you can use and understand what suits you best in terms of efficiency and speed.

Let me know if you know any better method with code than the one discussed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python Get Filename From A Path Without Extension 4 Ways
  2. How To Concatenate String and Integer In Python
  3. Code Example: Convert a String to DateTime in Python
  4. How Do I Sort A Dictionary By Value In Python
  5. Code Example: How To Add Numbers In A List Python
  6. Python – How To Delete All Files In A Directory
  7. How To Remove Non-alphanumeric Characters From String In Python

Leave a Comment