Question Paper
Question Paper
A B C
0 1 2 3
1 5 6 8
Pandas DataFrame
Q.8. ----------------- methodin Pandas is used to change/modify the index of rows and coloums of
a DataFrame.
Import numpy as np
Import pandas as pd
A = np.array ([35,40,71,25])
B = np.rray ([27,34,56,73])
C = [11,22,33,44,]
DF + pd. DataFrame ([A,B,C,]) Print (DF)
Q. 12. Write the code in python to create dataframe form given list
Q.14. Aman store some data in the form of nested list. Later on he created datafframe “DF”
Form the list given below by writing the following code. How many columns will be there in “DF”
L1 = [ “ Aman “ , Cricket “ , “ 7th “ ],
[ “Ankit” , “ Hockey” , “ 11th ” ]
[ “Sunita” , “ Bastket ball ” , “ 9th ” ]
DF = pd. DataFrame (L1) Print (DF)
Q. 15. Which attribute of DataFrame is used to give user defined index value?
Q.16. Which attribute of DataFrame is used to give user defined column name?
Q.17. Complete the following code to get the Output given below:
Import pandas as
_________________________
L1 = [“Aman” , 45], [“Ankit” , 56], [“_____________”, 67]
DF = pd. ________________(L1,_____________= “Name”,
“Marks” ] ,index = [___________])Print (DF)
OUTPUT”
NAME MARKS
1 AMAN 45
2 ANKIT 56
3 SUNITA 67
Q.18. Complete the following code to get the Output given below:
Import pandas as
_________________________
L1 = [“Name” : [“Aman” , [“Aman” , “Ankit” , “Sunita “], “Mark” [45,56,67]
DF = pd. DataFrame(L1,Columns = [________________] , index = [1,2,3])
Print (DF)
OUTPUT”
MARKS NAME
1 AMAN 45
2 ANKIT 56
3 SUNITA 67
Q.19. Consider the code given below and answer the following question
Ld = [{‘a’ : 10, ‘b’ : 20} , {‘a’ :5, ‘b’ : 10, ‘c’ : 20}]
DF = pd. DataFrame (Ld)
Print (DF)
A. How many rows will be there in dataframe “DF”
B. How many columns will be there in dataframe “DF”
C. How many NaN will be there in dataframe “DF”
D. Write the missing import statement in the above code.
E. How many dictionaries are used in the above code.
Q.23. Name the function which is used to display first n rows in the DataFrame.
Q.26. There are 7 rows in a dataframe “DF”. How many rows will be displayed by the following
Statement? DF.head(10)
Q28. Write a statement in python to add a new column “Marks” with values (23, 34, 45, 12) in a
Dataframe “DF”
A B C
0 1 2 3
1 5 6 8
Pandas DataFrame
a. Write a statement to change values of column “C”. New values are (4,9)
b. Write a statement to change all the values of column “B” to 0
c. Write a statement to add a new row with value (7,8,9)
d. Write a statement that will return the result (2,3)
e. Write the output of the statement : DF. Size( )
Import pandas as pd
Import numpy as np
Name = ______________.array([ ‘Anil’, ‘Sumit’ , ‘ Ankit’ , ‘Ananya’])__________=
__________.DataFrame ( ) Print (DF)
Name Class
0 Anju 7
1 Suman 11
Pandas DataFraem
Q.32. Consider the above dataframe “DF” (Q.31) and write the code to add a new column “ Roll no”
With values (21 ,22)
Q.33. Consider the above dataframe “DF” (Q.31) and write the code to update the value of column “
“Roll no” , New values are (23 , 27)
Q.34. Consider the above dataframe “DF” (Q.31) and write the code to Change the values of columns
“Class” to “12”
Q.35. Consider the above dataframe “DF” (Q.31) and write the code to change the index values of
Both rows. Index value 0 and 1 should be replaced by “First” and “Second” respectively.
Q.37. Aman wants to add a new row with values (‘Suman” , 9 , 11) at the end of the dataframe “df”
He does not know the total number of rows available in dataframe. As a friends of Aman, help
Him to write the code.
Q.38. Which of the following code will change the value of entire row to “0” in dataframe “DF”
Code A : DF. Loc[ “preeti’ ] = 0
Code B : DF [ ‘preeti” ] = 0
Q.39. When we try to add a row with lesser values than the number of columns in the DataFrame,
It results in a ___________________error.
Q.40. When we try to add a column with lesser values than the number of row in the DataFrame,
It results n a _______________error.
Q.41. We can use the _____________________method to delete rows and columns form a
DataFraem.
Q.42. Write the code to delete the row with label ‘Book’ form dataframe “DF”
Q.43. Which parameter of drop ( ) function is used to specify the row or column to be delete?
Q.44. Write the code to delete the column with label ‘Marks’ form dataframe “DF”.
Q.45. Write the code to delete the column with label ‘Marks’ ‘Class’ and ‘Roll no’ from dataframe “DF”
Q.46. A) Raman wants to create a pandas dataframe form the data given below. He wants to give
Column name as “ Emp no “ and “Ename”. Help him to write the code.L1 = [1, “Anil”]
[2, Sunil”] , [3, “ Suman” ]
Q.46. B) Raman wants to display the column “ Emp no” fpr, dataframe “DF”. He writes the following
Code which is not working. Help him to correct the error.
Q.46.C) ‘Raman’ s friend Shreya ask him to add a new column “Salary” in dataframe “DF” with
Values ( 10000, 20000, 25000). Help him to write the code.
Q.46.D) What type of error is returned by the following statement written by Raman?
DF = DF.drop( “Sal” , axis = 1)
Q.46. F) Raman wants to change the row label 0,1,2, to “One” , “ Tow” , “ Three” respectively
Help him to write the code.
Q.47. Write two ways of indexing dataframe.
Q.50. Which function in pandas dataframe is used to insert a new column at specific position?
Q.51. Name two functions/statement which are used to delete column form pandas dataframe?
Q. 53. Consider the pandas dataframe “DF” given below:
Arnab Ramit
Samridhi Riya Mallika
Maths 90 82
89 81 94
Science 91 81
91 71 95
English 85 86
83 80 90
Hindi 97 96
88 67 99
a. Write the code to insert column “Amit” with (90, 8, 92, 959) at position 5 (after Malllika )
b. Write the code to insert column “Amit” with values ( 92, 92, 95, 89) a position 2.
c. Write the code to delete column “Riya” form dataframe “DF”
d. Write the code to delete columns “Ramit” and “Riya” form dataframe “DF”
e. Following two statements will give same or different result.
Pint(DF.loc[0 : 1])
Print(DF.loc[“Maths” : “Maths”])
f. Write the statement to display marks of “Amit” in “Maths”.
g. Aman has written the following command to delete the column “Amit” form dataframe
“DF”. Output is not coming . Help him to correct the code.
DF.drop(“Amit”)print(DF)
h. Write a program to write dataframe “DF” to “Data.csv”
Q.54. Impot pandas as pd
A. = {‘Sunil’ : 28, ‘Raman’ : 20, ‘Kamal’ : 19, ‘Ashu’ : 26}
B. = {‘Kamal’ : 34, ‘Ram’ : =33, ‘ Parth’ : 38, ‘Ashu’ : 20}
Table = {‘Sem – l’ : A, ‘Sem-II’ : B}
Df = pd.DataFrame(table)
Print(df.loc[‘Kamal’ : Ram’]) print(df)
Q.55. Write a Python code to create a DataFrame with appropriate column headings form the list given
below: [1, ‘Anil’ , ‘X’ ], ] [2, ‘Sunil’ , ‘XI’ ],
[3, ‘Ravi’ , ‘X’ ] , [4, ‘Ashu’ , ‘XI’]]
df = pd.DataFrame(L, columns=[‘Ralno’ , ‘Name’, Class’ ])print(df)
Q56. Consider the given DataFrame ‘Stocks’ :
Name price Quantity
0 Ram 890 10
1 Register 395 12
2 Keyborad 350 5
3 Mouse 250 17
Write suitable Python statements for the following:
1. Add a column calld ‘Total’ which is product of price and Quantity
2. Add a new item named “ Scanner” having price 3250 and Quantity as 10.
3. Remove the column Quantity
Q.57. Dhiriti is working in a company where she was asked to create the following dataframe. Help her
Writing the code to create the dataframe ‘df’ with ‘W! , ‘W2’ , ‘W3’ , ‘W4’ Index value and also
Write the answer of he following task.
Wing Desktop Labtop Table
W1 Pre-Primary 15 5 2
W2 Primary 22 10 3
W3 Middle 18 8 4
W4 Senior 30 15 7
1. Write a Python statement to prin number of Desktop and Tablet in Middle and Secondary
Wing.
2. Write a Python statement to print Wing which hve ‘ Desktop’ more than 20.
3. Write a Python statement to print Wing and numbr of Laptop which have ‘Desktop’ more
than 20
Q. 59. Read the following code:
Import pandas as pd
L = [[‘Pre-Primary’, 15, 5, 2], [‘Primary’ , 22,10,3], [Middle’ , 18,8,4], [‘Senior’ , 30,15,7]
Df = [d. DataFrame,(L, columns = ‘Tablet’])print(df)
Answer the question given below:
1. What is the shape of the dataframe df?
2. Name the index and column names off dtaframe df
3. Write a statement in pythonto add index value as [‘W1’ , ‘W2’ , ‘W3’ , ‘W4’ , ‘W5’ , ]
4. Write a Python statement to calculate Laptop + Desktop + Tablet and display it as ‘Total
Systems’ in a dataframe.
5. Write the output of the following statements
Print(df) Print(“_________________________________”)
Print(df.loc[ ;, ‘Desktop’])print(“_________________________________”)
Print(df.loc[: , ‘Desktop’ , : ‘Laptop’])print(“________________________”)
Print(df.iloc[1:, 3,0 : 2])print(“___________________________________”)
Print(df.iloc[0:1, 1: 2])print(“____________________________________”)
Print(df[1 : 3])print(“____________________________________”)
Q.60. Consider the following code and answer the following.
Import pandas as pd
D1 = {‘C1 : ‘UK’ ,C2’ : ‘USSR’, ‘C3’ : ‘USA’}
D2 = (‘S1’ : ‘UP’ , ‘S2’ : ‘Delhi’}
City = {1 : D1, 2 : D2}
Df = pd.DataFrame(city)print(df)