Iteration in Pandas – 3 Unique Ways to Iterate Over DataFrames
Get Job-Ready: Data Analysis using Python with 70+ Projects Start Now!!
Iterating over a dataset allows us to travel and visit all the values present in the dataset. This facilitates our grasp on the data and allows us to carry out more complex operations. There are various ways for Iteration in Pandas over a dataframe. We can go, row-wise, column-wise or iterate over each in the form of a tuple.
Iteration in Pandas
With Pandas iteration, you can visit each element of the dataset in a sequential manner, you can even apply mathematical operations too while iterating. But, before we start iteration in Pandas, let us import the pandas library-
>>> import pandas as pd
Using the .read_csv function, we load a dataset and print the first 5 rows.
>>> dataflair = pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv") >>> dataflair.head()
Output-
Month “1958” “1959” “1960”
0 JAN 340 360 417
1 FEB 318 342 391
2 MAR 362 406 419
3 APR 348 396 461
4 MAY 363 420 472
Don’t forget to check – 5 Pandas Options to Customize Your DataÂ
3 Ways for Iteration in Pandas
There are 3 ways to iterate over Pandas dataframes are-
- iteritems(): Helps to iterate over each element of the set, column-wise.
- iterrows(): Each element of the set, row-wise.
- itertuple(): Each row and form a tuple out of them.
1. iteritems() in Pandas
The function iteritems() lets us travel and visit each and every value of the dataset.
>>> for key,values in dataflair.iteritems(): ... print(key, values) …
We see in the output that because of iteritems(), our code snippet runs through each and every element in all the columns of the dataset.
Output-
Month 0 JAN
1Â Â FEB
2Â Â MAR
3Â Â APR
4Â Â MAY
5Â Â JUN
6Â Â JUL
7Â Â AUG
8Â Â SEP
9Â Â OCT
10Â NOV
11Â Â DEC
Name: Month, dtype: object
“1958” 0 340
1Â Â 318
2Â Â 362
3Â Â 348
4Â Â 363
5Â Â 435
6Â Â 491
7Â Â 505
8Â Â 404
9Â Â 359
10Â 310
11Â 337
Name: “1958”, dtype: int64
“1959” 0 360
1Â Â 342
2Â Â 406
3Â Â 396
4Â Â 420
5Â Â 472
6Â Â 548
7Â Â 559
8Â Â 463
9Â Â 407
10Â 362
11Â 405
Name: “1959”, dtype: int64
“1960” 0 417
1Â Â 391
2Â Â 419
3Â Â 461
4Â Â 472
5Â Â 535
6Â Â 622
7Â Â 606
8Â Â 508
9Â Â 461
10Â 390
11Â 432
Name: “1960”, dtype: int64
Do you know how to create pandas panel?
2. iterrows() in Pandas
With iterrows() we can visit all the elements of a dataset, row-wise.
>>> for row_index,row in dataflair.iterrows(): ... print(row_index, row) ...
Output-
0 Month JAN
“1958”  340
“1959”  360
“1960”  417
Name: 0, dtype: object
1 Month FEB
“1958”  318
“1959”  342
“1960”  391
Name: 1, dtype: object
2 Month MAR
“1958”  362
“1959”  406
“1960”  419
Name: 2, dtype: object
3 Month APR
“1958”  348
“1959”  396
“1960”  461
Name: 3, dtype: object
4 Month MAY
“1958”  363
“1959”  420
“1960”  472
Name: 4, dtype: object
5 Month JUN
“1958”  435
“1959”  472
“1960”  535
Name: 5, dtype: object
6 Month JUL
“1958”  491
“1959”  548
“1960”  622
Name: 6, dtype: object
7 Month AUG
“1958”  505
“1959”  559
“1960”  606
Name: 7, dtype: object
8 Month SEP
“1958”  404
“1959”  463
“1960”  508
Name: 8, dtype: object
9 Month OCT
“1958”  359
“1959”  407
“1960”  461
Name: 9, dtype: object
10 Month NOV
“1958”  310
“1959”  362
“1960”  390
Name: 10, dtype: object
11 Month DEC
“1958”  337
“1959”  405
“1960”  432
Name: 11, dtype: object
Have you checked – Which Industry Segments are using Python Pandas?
3. itertuples() in Pandas
The function itertuples() creates a tuple for every row in the dataset. Thus iterating over it would give us a tuple of the rows present.
>>> for row in dataflair.itertuples(): ... print(row) ...
Output-
Pandas(Index=0, Month=’JAN’, _2=340, _3=360, _4=417)
Pandas(Index=1, Month=’FEB’, _2=318, _3=342, _4=391)
Pandas(Index=2, Month=’MAR’, _2=362, _3=406, _4=419)
Pandas(Index=3, Month=’APR’, _2=348, _3=396, _4=461)
Pandas(Index=4, Month=’MAY’, _2=363, _3=420, _4=472)
Pandas(Index=5, Month=’JUN’, _2=435, _3=472, _4=535)
Pandas(Index=6, Month=’JUL’, _2=491, _3=548, _4=622)
Pandas(Index=7, Month=’AUG’, _2=505, _3=559, _4=606)
Pandas(Index=8, Month=’SEP’, _2=404, _3=463, _4=508)
Pandas(Index=9, Month=’OCT’, _2=359, _3=407, _4=461)
Pandas(Index=10, Month=’NOV’, _2=310, _3=362, _4=390)
Pandas(Index=11, Month=’DEC’, _2=337, _3=405, _4=432)
Summary
Hopefully, the above-given Pandas tutorial helped you understand the various methods of accessing and iterating over your dataset. We used iteritems() for column-wise, iterrows() for row-wise, and itertuple() for each row and form a tuple out of them. This simplifies the process of operating on your dataset.
Let’s dive into Pandas more deeper with Pandas Function Applications
All queries in your mind related to “Iteration in Pandas” should be redirected into the comments below.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google