Important QnA iterrows iteritems class 12 IP

In this article Important QnA iterrows iteritems class 12 IP, you will get some important questions based on the topic iterrows(), iteritems(), insert index, row and column for class 12 IP. 3

Before starting questions and answers click here tor read the notes.

Watch this video for more understanding:

Important QnA iterrows iteritems class 12 IP

Let us begin this article QnA iterrows iteritems class 12 with short answer questions.

Short Answer questions / Conceptual Questions

Q 1. Explain iterrows() function with example.
Ans.: The iterrows() function is used to iterate values by rows indexes. For example, Our school subject wise highest marks of the past 3 years are considered in the following data frame and accessed row-wise with the help of iterrows() function. 
import pandas as pd
hm_past3={2018:{'English':98,'Physics':99,'Chemistry':100,
'Biology':100,'Maths':100,'CS':100},
2019:{'English':99,'Physics':97,'Chemistry':99,
'Biology':100,'Maths':100,'CS':99},
2020:{'English':97,'Physics':98,'Chemistry':98,
'Biology':98,'Maths':99,'CS':96},
}
df1=pd.DataFrame(hm_past3)
for i, row in df1.iterrows():
print("-----------------------")
print(row)
Output
iterrows
 
Q – 2 Print the text in front of Rows like ‘highest marks in [Year]” and column heading “Subject:[Subject]”
Ans.: 
import pandas as pd
hm_past3={2018:{'English':98,'Physics':99,'Chemistry':100,
'Biology':100,'Maths':100,'CS':100},
2019:{'English':99,'Physics':97,'Chemistry':99,
'Biology':100,'Maths':100,'CS':99},
2020:{'English':97,'Physics':98,'Chemistry':98,
'Biology':98,'Maths':99,'CS':96},
}
df1=pd.DataFrame(hm_past3)
for i, row in df1.iterrows():
print("-----------------------")
print("Subject:",i)
y=2018
for vl in row:
print("Highest marks of ",y,":",vl)
y += 1
Output
Access values using iterrows  with custom message
 
Q -3 Write a program to print each data value along with its row index and column name using iterrows(). [Consider the same data given in the above example]
import pandas as pd
hm_past3={2018:{'English':98,'Physics':99,'Chemistry':100,
'Biology':100,'Maths':100,'CS':100},
2019:{'English':99,'Physics':97,'Chemistry':99,
'Biology':100,'Maths':100,'CS':99},
2020:{'English':97,'Physics':98,'Chemistry':98,
'Biology':98,'Maths':99,'CS':96},
}
df1=pd.DataFrame(hm_past3)
for i, row in df1.iterrows():
print("-----------------------")
ix=row.index
a = 0
for vl in row:
print("[",i,"][",ix[a],"]:",vl)
a += 1
Output:
Output Iterrows with index and column
 

Q – 4 Write a program to iterate over a dataframe and print data using iteritems(). [Consider data given above]
import pandas as pd
hm_past3={2018:{'English':98,'Physics':99,'Chemistry':100,
'Biology':100,'Maths':100,'CS':100},
2019:{'English':99,'Physics':97,'Chemistry':99,
'Biology':100,'Maths':100,'CS':99},
2020:{'English':97,'Physics':98,'Chemistry':98,
'Biology':98,'Maths':99,'CS':96},
}
df1=pd.DataFrame(hm_past3)
for i, col in df1.iteritems():
print("-----------------------")
print(col)

Output

iteritems
 
 
Q – 5 In the given dataframe customer’s records stored. Write a program to calculate the total purchase done by them and assigned a category like “Gold”, “Silver” and “Platinum”. Consider the following criteria to assign a category.
bill_amt 0 to 1000 👉 Platinum
bill_amt 1001 to 5000 👉 Silver
bill_amt >5001 👉 Gold
import pandas as pd
def dframe():
dt = {'cname':['Aman','Chaman','Baman'],
'Jan':[1100,3500,2500],
'Feb':[100,500,200],
'March':[700,250,600]
}
cust_df=pd.DataFrame(dt)

for (i, r) in cust_df.iterrows():
t_amt=r['Jan']+r['Feb']+r['March']
if(t_amt>0 and t_amt<=1000):
cat='Platinum'
elif(t_amt>1000 and t_amt<=3000):
cat='Silver'
elif(t_amt>3000 and t_amt<=5000):
cat='Gold'
print(r['cname'],":",t_amt,"-",cat)
Output:
Customer 2BCategory
 
Q – 6 Consider the above data and do the following:
(a) Add new row at the last: Jaman, 2300, 500, 600
(b) Add these three rows: [Gaman, 1100, 300, 1200] – [Kaman,2200,700,400] – [Laman,1200,1800,750]
(c) Add this data at to row number 5: [Naman,900,1300,700]
(d) Add column with title “total” at right side with total amount
(e) Add column with title “City” after name 
 
Ans:
(a) cust_df=cust_df.append({‘Cname’:’Jaman’,’Jan’:2300,’Feb’:500,’March’:600})
 
(b) To add multiple rows, first generate list of series with data and set dataframe columns as index of series. Then use append method. Look at the following code:
new_cols=[pd.Series([‘Gaman’,1100,300,1200],index=cust_df.columns),pd.series[‘Kaman’,2200,700,400],index=cust_df.columns),[‘Laman’,1200,1800,750],index=cust_df.columns)]
cust_df=df.append(new_cols,ignore_index=True)
 
(c) cust_df.loc[5]=[‘Naman’,900,1300,700]
 
(d) cust_df[‘Total’]=t_amt 
Note: Considered previous code for total
 
(e) cust_df.insert(2,”City”,[“Ahmedabad”,”Baroda”,”Surat”,”Bharuch”],True)
Note: Add the value as much as the data you have in the dataframe.
 
That’s all from the article QnA iterrows iteritems class 12. If you have any doubt or queries regarding this article QnA iterrows iteritems class 12 feel free to ask in the comment section. 

Follow this link to read all the contents of IP class 12.

Informatics Practices class 12

Thank you for reading this article QnA iterrows iteritems class 12.

Comment Your Views

%d bloggers like this: