100+ Important Pandas Dataframe Questions with Solutions Class 12 IP

Share with others

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe
Pandas Dataframe

Q1. What is DataFrame?

Q2. Write code to create empty DataFrame named ‘DF1’.

 

Q3. Which method is used to create DataFrame in Python?

Q4. Write two differences between Series and DataFrame.

Q5. Create the following dataframe using List of Dictionaries.

ABC
0123
1568
Pandas DataFrame
 


Pandas Dataframe
Pandas Dataframe

Pandas Dataframe Questions Class 12 IP

Q6. Which data types can be used to create DataFrame?

Q7. Which library is to be imported to create dataframe from Numpyndarrays ?

Q8. _______ method in Pandas is used to change/modify the index of rows and columns of a Dataframe

Q9. Give an example to create dataframe from a single ndarray.



Q10. Give an example to create dataframe from two ndarray.



Pandas Dataframe
Pandas Dataframe

Pandas Dataframe Questions Class 12 IP

Q11. Write the output of the following code:

import numpy as np
import pandas as pd
A = np.array([35, 40, 71, 25])
B = np.array([27, 34, 56, 73])
C = [11, 22, 33, 44]
DF = pd.DataFrame([A, B, C])
print(DF)


Q12. Write the code in python to create dataframe from given list.

L1 = [“Anil”, “Ruby”, “Raman”, “Suman”]

L2 = [35, 56, 48, 85]

Q13. Fill in the blank to produce the Output.

import pandas as pd
L1 = ["Anil", "Ruby", "Raman", "Suman"]
L2 = [35, 56, 48, 85]
DF = pd.DataFrame([L1, L2], ___________________________________ )
print(DF)

OUTPUT

     Ist    IInd    IIIrd       IVth
a  Anil  Ruby  Raman  Suman
b    35    56     48        85

Q14. Aman store some data in the form of nested list. Later on he created dataframe “DF” from 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”, “Basket ball”, “9th”]]

import pandas as pd
L1 = [["Aman", "Cricket", "7th"],  ["Ankit", "Hockey", "11th"],  ["Sunita", "Basket ball", "9th"]]
DF = pd.DataFrame(L1)
print(DF)

Q15. Which attribute of DataFrame is used to give user defined index value?

Pandas Dataframe
Pandas Dataframe

Pandas Dataframe Questions Class 12 IP

Q16. Which attribute of DataFrame is used to give user defined column name?

Q17. 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

Q18. Complete the following code to get the Output given below:

import pandas as pd
L1 = {"Name" : ["Aman", "Ankit", "Sunita"], "Marks" : [45, 56, 67]}
DF = pd.DataFrame(L1, columns = [______________________], index = [1, 2, 3])
print(DF)



OUTPUT :

          Marks               Name             
1            45                 Aman             
2           56                 Ankit                
3           67                 Sunita              

Q19. Consider the code given below and answer the following questions:

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.

Q20. Give an example of creating dataframe from two series.

Pandas Dataframe Questions Class 12 IP

Q21. Write five attributes of DataFrame.

Q22. Which attribute of dataframe is used for the following.

  1. To display row labels.
  2. To display column labels.
  3. To display data type of each column in the DataFrame.
  4. To display all the values in the DataFrame.
  5. To display number of rows and columns in a tuple.
  6. To display total number of values in the dataframe.
  7. To transpose the dataframe.
  8. To returns the value True if DataFrame is empty and False otherwise

Q23. Name the function which is used to display first n rows in the DataFrame.

Q24. Write a statement to display first 7 rows from dataframe “DF”.

Q25. Write a statement to display last 7 rows from dataframe “DF”.

Pandas Dataframe Questions Class 12 IP

Q26. There are 7 rows in a dataframe “DF”. How many rows will be displayed by the following statement?

DF.head(10)

Q27. What is the default value of ‘n’ in tail(n) function?

Q28. Write a statement in python to add a new column “Marks” with values (23, 34, 45, 12) in a dataframe “DF”

Q29. Consider the following dataframe “DF”

ABC
0123
1568
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( )

Pandas Dataframe Questions Class 12 IP

Q30. Fill in the blanks

import pandas as pd
import numpy as np
Name = ______________.array([‘Anil’, ’Sumit’, ’Akhil’, 'Ananya']) 
__________ = ________.DataFrame( )
print(DF)


Q31. Write two methods of creating the following dataframe “DF”.

NameClass
0Anju7
1Suman11
Pandas DataFrame


Q32. Consider the above dataframe “DF” (Q. 31) and write the code to add a new column “Roll no” with values (21, 22)



Q33. Consider the above dataframe “DF” (Q. 31) and write the code to update the value of column “Roll No”. New values are (23, 27)



Pandas Dataframe Questions Class 12 IP

Q34. Consider the above dataframe “DF” (Q. 31) and write the code to change the values of column “Class” to “12”



Q35. 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.

Q36. Which function/method is used to add a new row in dataframe?

Q37. 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 friend of Aman, help him to write the code.

Q38. 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

Q39. When we try to add a row with lesser values than the number of columns in the DataFrame, it results in a ____________ error.

Q40. When we try to add a column with lesser values than the number of rows in the DataFrame, it results in a ____________ error.

Pandas Dataframe Questions Class 12 IP

Q41. We can use the ___________________ method to delete rows
and columns from a DataFrame.

Q42. Write the code to delete the row with label ‘Book’ from dataframe “DF”

Q43. Which parameter of drop( ) function is used to specify the row or column to be delete?

Q44. Write the code to delete the column with label ‘Marks’ from dataframe “DF”.

Q45. Write the code to delete the columns with label ‘Marks’, ‘Class’ and ‘Rollno’ from dataframe “DF”.

Pandas Dataframe Questions Class 12 IP

Q46. a) Raman wants to create a pandas dataframe from 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”]]

Q46. b) Raman wants to display the column “Emp no” from dataframe “DF”. He writes the following code which is not working. Help him to correct the error.

print(DF.Emp no)

Q46. 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.

Q46. d) What type of error is returned by the following statement written by Raman?

DF = DF.drop(“Sal”, axis = 1)

Q46. e) Raman wants to change the column label “Salary” to “Salaries”. Help him to write the code.

Q46. f) Raman wants to change the row label 0, 1, 2 to “One”, “Two”, “Three” respectively. Help him to write the code.

Pandas Dataframe Questions Class 12 IP

Q47. Write two ways of indexing dataframe.

Q48. Consider the pandas dataframe “DF” given below:

            Emp no      Ename
One         1             Anil
Two         2             Sunil
Three      3             Suman

Write the output of :

a. print(DF.loc[“Two”])



b. print(DF.loc[ : , “Ename”])



c. print(DF[“Ename”])



d. print(DF.loc[“One”, “Two”])

e. print(DF.loc[[“One”,”Two”]])



Pandas Dataframe Questions Class 12 IP

f. print(DF[“Emp no”,”Ename”])

g. print(DF[[“Emp no”,”Ename”]])

Pandas Dataframe Questions Class 12 IP

Q49. Consider the pandas dataframe “DF” given below:

               Arnab                 Ramit                    Samridhi            Riya             Mallika
Maths      90                          92                        89                       81               94
Science    91                          81                         91                        71               95
English    85                          86                        83                       80               90
Hindi       97                          96                        88                       67               99

Write the output of :

a. print(DF.loc[“Maths”] > 90)



b. print(DF.loc[ : , “Arnab”] > 90)

c. print(DF.loc[‘Maths’ : ‘Science’])

d. print(DF.loc[‘Hindi’ : ‘English’])

e. Following two statements will produce the same result ?(Yes/No)

  1. print(DF)
  2. print(DF.loc[‘Maths’ : ‘Hindi’])

Pandas Dataframe Questions Class 12 IP

f. print(DF[[‘Arnab’ , ‘Ramit’ , ‘Riya’]])

g. Write a statement to display marks of “Arnab” and “Ramit” of subjects “Maths”, “Science” and “English”.

h. Write the statement to display Marks of “Arnab” in “Maths”.

i. Write the statement to display marks of “Riya” in all subjects.

j. Write the statement to display marks of Ramit, Samridhi and Riya of “English” Subject.

k. Write the output of the following statement:

print(DF.loc[[True , False , True , False]])

Pandas Dataframe Questions Class 12 IP

l. Write a statement to display the first record of pandas dataframe “DF”.

m. Write a statement to display the last three records of pandas dataframe “DF.

n. Write the output of : print(DF.shape)

o. Write the output of : print(DF.ndim)

p. Write a statement to get the following as output :

                    Maths  Science  English  Hindi
Arnab                90       91       85           97
Ramit                92       81       86           96
Samridhi           89       91       83           88
Riya                  81        71       80           67
Mallika             94       95       90           99

q. Write the output of : print(DF.size)

Pandas Dataframe Questions Class 12 IP

r. Write the output of : print(DF.empty)

s. Write the output of the following statement

DF1 = DF.loc['Maths' : 'Science' , ['Arnab' , 'Samridhi']] 
print(DF1 * 2)


t. Write the output of the following statement

DF1 = (DF.loc['Hindi' : 'Hindi' , 'Riya' : 'Riya']) 
print(DF1 % 2)

u. Write the code to find the highest marks in subject “Hindi”



v. Write the code to find the lowest marks in subject “English”



Pandas Dataframe Questions Class 12 IP

w. Write the output of the statement : print(min(DF))

x. Write the output of the following code:

DF1 = DF.loc['English' : 'Hindi']
DF2 = DF.loc['English' : 'Hindi']

print(DF1 + DF2)

y. Write the output of the following :

DF1 = DF.loc['English' : 'Hindi']
DF2 = DF.loc['Maths' : 'Science']
DF1 = DF1.append(DF2)
print(DF1)

z. Write the output of the following :

DF1 = DF.loc['English' : 'Hindi']
DF2 = DF.loc['Maths' : 'Science']
DF1 = DF1.append(DF2, sort = "True")
print(DF1)

Pandas Dataframe Questions Class 12 IP

Q50. Which function in pandas dataframe is used to insert a new column at specific position?

Q51. Name two functions/statement which are used to delete column from pandas dataframe?

Q52. Write two differences between del statement and drop() function of pandas dataframe.

Pandas Dataframe Questions Class 12 IP

Q53. Consider the pandas dataframe “DF” given below:

               Arnab                 Ramit                    Samridhi            Riya             Mallika
Maths      90                          92                        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 values (90, 92, 95, 89) at position 5 (after Mallika)

b. Write the code to insert column “Amit” with values (90, 92, 95, 89) at position 2.

c. Write the code to delete column “Riya” from dataframe “DF”

d. Write the code to delete columns “Ramit” and “Riya” from dataframe “DF”

e. Following two statements will give same or different result.

print(DF.iloc[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” from 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”

Q54. Write a program to create dataframe “DF” from “data.csv”

Term 1 – IP Sample Paper Class 12 2021


1. IP Sample Paper Class 12 2021 with Solutions Paper 1

2. IP Sample Paper Class 12 2021 with SolutionsPaper 2

3. IP Sample Paper Class 12 2021 with SolutionsPaper 3 (Coming Soon)


Important Links

100 Practice Questions on Python Fundamentals

120+ MySQL Practice Questions

90+ Practice Questions on List

50+ Output based Practice Questions

100 Practice Questions on String

70 Practice Questions on Loops

120 Practice Questions of Computer Network in Python

70 Practice Questions on if-else

40 Practice Questions on Data Structure

Pandas Dataframe Questions Class 12 IP

Computer Science Syllabus 2021-2022.

Informatics Practices Syllabus 2021-2022

Class 12 Computer Science Chapter wise MCQ

Pandas Dataframe Questions Class 12 IP


Share with others

Leave a Reply

%d bloggers like this: