Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Let’s see the how to iterate over rows in Pandas Dataframe using
Method #1: Using the
This method iterated over the rows as (index, series) pairs.
Original DataFrame: age name 0 10 Sujeet 1 11 Sameer 2 12 Sumit Rows iterated using iterrows() : Sujeet 10 Sameer 11 Sumit 12
Method #2: Using the
This method returns a named tuple for every row.
getattr() function can be used to get the
row attribute in the returned tuple. This method is faster than Method #1.
Original DataFrame: age name 0 10 Sujeet 1 110 Sameer 2 120 Sumit Rows iterated using itertuples() : Sujeet 10 Sameer 110 Sumit 120
There are few other ways we can iterate over rows in Pandas Dataframe. See Different ways to iterate over rows in Pandas Dataframe.
- Different ways to iterate over rows in Pandas Dataframe
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Get all rows in a Pandas DataFrame containing given substring
- Pandas - How to shuffle a DataFrame rows
- Ranking Rows of Pandas DataFrame
- How to Select Rows from Pandas DataFrame?
- Sorting rows in pandas DataFrame
- How to randomly select rows from Pandas DataFrame
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Create a list from rows in Pandas DataFrame | Set 2
- Iterating over rows and columns in Pandas DataFrame
- Create a list from rows in Pandas dataframe
- How to get rows/index names in Pandas dataframe
- Dealing with Rows and Columns in Pandas DataFrame
- Selecting rows in pandas DataFrame based on conditions
- Count the number of rows and columns of Pandas dataframe
- How to drop rows in Pandas DataFrame by index labels?
- Count the number of rows and columns of a Pandas dataframe
- Count all rows or those that satisfy some condition in Pandas dataframe
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.