Looping through Data Frame in Python
Iterate over Rows and Columns in Pandas Dataframe
This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are rows and columns in the Data Frame of Pandas library in Python, and How can we iterate over the rows and columns in Pandas Data Frame.
What are Pandas in Python?
Pandas is a very widely used library in Python. It is an open-source, potent data analysis tool embedded into a library written in Python. The Pandas library leverages the power and the speed to its peak extent to make the data analysis and processing easier for Big Data domain people to make their jobs easier, mainly the Data Scientists. As a fact good to know since the Pandas can be visualized similar to Microsoft Excel sheet; Excel is written in Python too.
Understanding Python Pandas Data Structures
The Pandas library can be broadly distinguished mainly into two main sub-categories: Series and Data Frame models.
- Series Data Structure in Pandas: It is a one-dimensional array with an indexing feature. The Series Data Structure in Python stores a single column or only a single row of data in its Data Frame.
- Data Frame Data Structure in Pandas: It is usually represented in a tabular-like spreadsheet kind structure, which means rows which are each of which contains one or multiple columns to it.
A one-dimensional Array labelled is capable of holding any data series into it. A two-dimensional array is also labelled structure in nature with columns of potentially different data types in the Data Frame data structure in Pandas Python.
Iterating over the Rows and Columns in Pandas Data Frame
To iterate over a Data Frame in Pandas Python, we need to understand how can we iterate over a Dictionary in Python because we iterate over the keys in the dictionary in which each key consists of a particular value; the same methodology we follow to iterate over a Data Frame in Pandas library in Python.
Iterating over the elements in Pandas Data Frame is possible mainly in two ways, and they are
- Iterating over the rows.
- Iterating over the columns.
Iterating over the Rows
There are three in-built functions which will help us in iterating over the rows in Pandas Data Frame, which are iterrows(), itertuples(), and iteritems(); we will now see each function one by one
- Iterrows() Function
See the below code to understand the iterrows() function clearly.
Code:
#command for the importing pandas as pd
import pandas as pd
# dictionary of lists
dict_123 = {'name':["apsarasa", "pankaj tripathi", "sudhir babu", "Geeku ranjith"],
'degree': ["B.tech", "LLB", "CA", "M.tech"],
'score':[90, 40, 80, 98]}
# The Code to creating a dataframe from a dictionary
df_11 = pd.DataFrame(dict_123)
print(df_11)
Output:
name degree score
0 Apsarasa B.tech 90
1 Pankaj Tripathi LLB 40
2 Sudhir Babu CA 80
3 Geeku ranjith M.tech 98
- Iteritems() Function
Code:
#Command for the importing pandas as pd
import pandas as pd
# dictionary of lists
dict_123 = {'name':["apsarasa", "pankaj tripathi", "sudhir babu", "Geeku ranjith"],
'degree': ["B.tech", "LLB", "CA", "M.tech"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df_11 = pd.DataFrame(dict)
print(df_11)
Output
name degree score
0 Apsarasa B.tech 90
1 Pankaj Tripathi LLB 40
2 Sudhir Babu CA 80
3 Geeku ranjith M.tech 98
- Iteritems() Function
Code
#Command for importing pandas as pd
import pandas as pd
# dictionary of lists
dict_123 = {'name':["apsarasa", "pankaj tripathi", "sudhir babu", "Geeku ranjith"],
'degree': ["B.tech", "LLB", "CA", "M.tech"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df_11 = pd.DataFrame(dict_123)
# using iteritems() function to retrieve rows
for key, value in df_11.iteritems():
print(key, value)
print()
Output
Name
0 apsarasa
1 pankaj tripathi
2 sudhir babu
3 Geeku ranjith
Name: name, dtype: object
degree
0 B.tech
1 LLB
2 CA
3 M.tech
Name: degree, dtype: object
Score
0 90
1 40
2 80
3 98
Name: score, dtype: int64
Iterating over Columns
To iterate over the columns, we need to calculate the Data Frame columns; after that, we need to repeat to pull over the columns of the Data Frame.
See the code below to iterate over the columns in Data Frame Pandas Python
Code:
#Command for importing pandas as pd
import pandas as pd
# dictionary of lists
dict_123 = {'name':["apsarasa", "pankaj tripathi", "sudhir babu", "Geeku ranjith"],
'degree': ["B.tech", "LLB", "CA", "M.tech"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df_11 = pd.DataFrame(dict_123)
print(df_11)
Output:
name degree score
0 apsarasa B.tech 90
1 pankaj tripathi LLB 40
2 sudhir babu CA 80
3 Geeku ranjith M.tech 98
After getting the output we will now write a smaller code to iterate over the columns
Code:
df_11 = pd.DataFrame(dict_123)
#print(df_11)
columns = list(df_11)
for i in columns:
print(df_11[i][2])
Output:
sudhir babu
CA
80