Professional Documents
Culture Documents
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir",
"Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
print(df)
Now we apply iterrows() function in order to
get a each element of rows.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
for i, j in data.iterrows():
print(i, j)
print()
iteration over rows using iteritems()
In order to iterate over rows, we use iteritems() function this
function iterates over each column as key, value pair with the
label as key, and column value as a Series object.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir",
"Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
print(df)
Now we apply a iteritems() function in order
to retrieve an rows of dataframe.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir",
"Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
print(df)
Now we apply a itertuples() function inorder
to get tuple for each row
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
# using a itertuples()
for i in df.itertuples():
print(i)
# importing pandas module
import pandas as pd
for i in data.itertuples():
print(i)
• Iterating over Columns :
• In order to iterate over columns, we need to create a list of dataframe
columns and then iterating through that list to pull out the dataframe
columns.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir",
"Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
print(df)
Now we iterate through columns in order to iterate through
columns we first create a list of dataframe columns and then
iterate through list.
# creating a list of dataframe
columns
columns = list(df)
for i in columns:
col
Now we iterate over columns in CSV file in order to iterate over columns we create
a list of dataframe columns and iterate over list
for i in clmn:
# printing a third element of
column
print(col[i][2])
create a Dataframe
# import pandas package
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'A'),
('Swapnil', 22, 'B'),
('Priya', 22, 'B'),
('Shivangi', 22, 'B'),
]
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns =['Name', 'Age',
'Section'],
index =['1', '2', '3', '4'])
stu_df
• Method #1: Using DataFrame.iteritems():
Dataframe class provides a member function iteritems() which gives
an iterator that can be utilized to iterate over all the columns of a data
frame. For every column in the Dataframe it returns an iterator to the
tuple containing the column name and its contents as series.
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'A'), ('Swapnil', 22, 'B'), ('Priya', 22,
'B'),
('Shivangi', 22, 'B'),]