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”

Q54. Write the output of the following code:

import pandas as pd
A = {'Sunil' : 28, 'Raman' : 20, 'Kamal' : 19, 'Ashu' : 26}
B = {'Kamal' : 34, 'Ram' : 33, 'Parth' : 38, 'Ashu' : 20}
table = {'Sem-I' : A, 'Sem-II' : B}
df = pd.DataFrame(table)
print(df.loc['Kamal' : 'Ram'])
print(df)
Ans. 

          Sem-I      Sem-II
Kamal   19.0      34.0
Ashu    26.0      20.0
Ram      NaN    33.0


             Sem-I  Sem-II
Sunil       28.0     NaN
Raman   20.0     NaN
Kamal    19.0     34.0
Ashu     26.0     20.0
Ram      NaN    33.0
Parth    NaN     38.0

Q55. Write a Python code to create a DataFrame with appropriate column headings from the list given below

[1, ‘Anil’, ‘X’], [2, ‘Sunil’, ‘XI’], [3, ‘Ravi’, ‘X’], [4, ‘Ashu’, ‘IX’]

Ans. 

import pandas as pd
L=[[1, 'Anil', 'X'], [2, 'Sunil', 'XI'], [3, 'Ravi', 'X'], [4, 'Ashu', 'IX']]
df = pd.DataFrame(L, columns=['Rolno', 'Name', 'Class'])
print(df)

Q56. Consider the given DataFrame ‘stocks’:

      Name              Price      Quantity
0    RAM                890          10
1    Register           395          12
2    Key Board       350          5
3    Mouse            250           17

Write suitable Python statements for the following:

  1. Add a column called ‘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
Ans-1. stock['Total'] = stock['Price'] * stock['Quantity']
  
OR

stock['Total'] = stock.Price * stock.Quantity

2. stock.loc[4]=['Scanner', 3250, 10, 32500] #considered the column added above

3. stock = stock.drop('Quantity', axis=1)

Q57. Dhriti is working in a company where she was asked to create the following dataframe. Help her in writing the code to create the dataframe ‘df’ with ‘W1’, ‘W2’, ‘W3’, ‘W4’ index value and also write the answers of the following task.

WingDesktopLaptopTablet
W1Pre-Primary1552
W2Primary22103
W3Middle1884
W4Senior30157
  1. Predict the output of: print(df.shape)
  2. Write Python statement to display the value of column ‘Laptop’ and ‘Tablet’ of indexes W3 and W4.
  3. Add a new row in dataframe ‘df’ with index ‘W5’ and values are [‘Secondary’, 20, 15, 8]
  4. Write the output of: print(df.size)
  5. Write a statement in Python to delete row of ‘W3’.
  6. Write the output of: print(df.T)
Ans.

import pandas as pd
L=[['Pre-Primary', 15, 5, 2], ['Primary',22, 10, 3],['Middle', 18, 8, 4], ['Senior', 30, 15, 7]]
df = pd.DataFrame(L, columns = ['Wing', 'Desktop', 'Laptop', 'Tablet'], index=['W1', 'W2', 'W3', 'W4'])
print(df)

1. (4,4)
2. print(df.loc['W3' : 'W4' , 'Laptop' : 'Tablet'])
3. df.loc['W5']=['Secondary', 20, 15, 8]
4. 20
5. df=df.drop('W3', axis=0)
6. 
                      W1            W2            W4             W5
Wing     Pre-Primary  Primary      Senior       Secondary
Desktop         15             22             30               20
Laptop             5             10             15               15
Tablet              2               3               7                 8

Q58. Consider the following Dataframe ‘DF’ and answer the following questions

WingDesktopLaptopTablet
W1Pre-Primary1552
W2Primary22103
W3Middle1884
W4Secondary20158
W5Senior30157
  1. Write a Python statement to print number of Desktop and Tablet in Middle and Secondary Wing.
  2. Write a Python statement to print Wing which have ‘Desktop’ more than 20
  3. Write a Python statement to print Wing and number of Laptop which have ‘Desktop’ more than 20
Ans 
1.  print(df.loc['W3' : 'W4', ['Desktop', 'Tablet']])
2. print(df['Wing'][df.Desktop>20])
3. print(df[['Wing', 'Laptop']][df.Desktop>20])

Q59. 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 = pd.DataFrame(L, columns = ['Wing', 'Desktop', 'Laptop', 'Tablet'])
print(df)

Answer the questions given below:

1. What is the shape of the dataframe df?
2. Name the index and column names of dataframe df
3. Write a statement in Python to add index values 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("------------------------------------")
print(df.index)

Ans. 

1. (4,4) 
2. index = 0, 1, 2, 3 and columns = ['Wing', 'Desktop', 'Laptop', 'Tablet'] 
3. df.index = ['W1', 'W2', 'W3', 'W4', 'W5']
4. df['Total Systems'] = df['Laptop'] + df['Tablet'] + df['Desktop']
5. 

           Wing             Desktop        Laptop       Tablet
W1      Pre-Primary       15                  5                2
W2      Primary             22                10                3
W3       Middle              18                 8                4
W4       Senior              30                 15               7
------------------------------------
W1    15
W2    22
W3    18
W4    30
Name: Desktop, dtype: int64
------------------------------------
    Desktop  Laptop
W1       15       5
W2       22      10
W3       18       8
W4       30      15
------------------------------------
       Wing  Desktop
W2  Primary       22
W3   Middle       18
------------------------------------
    Desktop
W1       15
------------------------------------
         Wing     Desktop  Laptop  Tablet
W2  Primary       22      10       3
W3   Middle       18       8       4
------------------------------------
Index(['W1', 'W2', 'W3', 'W4'], dtype='object')

Q60. 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)
  1. Write the output of the above code
  2. Write the index value of above dataframe ‘df’
  3. Write the column names of above dataframe ‘df’
1. 
         1          2
C1    UK      NaN
C2  USSR    NaN
C3   USA    NaN
S1   NaN     UP
S2   NaN    Delhi

2. ['C1', 'C2', 'C3', 'S1', 'S2']   OR You can also write (C1, C2, C3, S1, S2)
3. [1, 2] OR Simply Write -- 1,2

Q61. Consider the following dataframe ‘df’

NamePrice
P1Pen10
P2Pencil5
P3Sharpner3

Write suitable Python statements for the following:

  1. Create the above dataframe in Python
  2. Add one more item named ‘Rubber’ with Price = 5 and index ‘P4’
  3. Add one column named ‘Quantity’ with values = [5, 9, 4, 6]
  4. Add one more column named ‘Total Price’ which can be calculated ‘Name’ * ‘Price’
  5. Write a statement to display ‘Price’ and ‘Quantity’ column of index ‘P2’
  6. Write a statement to display the detail of ‘P2’ and ‘P3’
  7. Remove the column ‘Quantity’
Ans 1. 

import pandas as pd 
D = [['Pen', 10],['Pencil', 5],['Sharpner', 3]]
df = pd.DataFrame(D, columns=['Name', 'Price'], index=['P1', 'P2', 'P3'])
print(df)

2. df.loc['P4'] = ['Rubber', 5]

3. df['Quantity'] = [5, 9, 4, 6]

4. df['Total Price'] = df['Price'] * df['Quantity']

5. print(df.loc[['P2'], ['Price', 'Quantity']])

6. print(df.loc['P2' : 'P3' , : ])

OR

print(df.loc[['P2','P3'],['Name','Price','Quantity','Total Price']])

OR

print(df.loc[['P2','P3'], : ])

OR

print(df[2:4])



7. df=df.drop('Quantity', axis=1)

Q62. Observe the following code

import pandas as pd 
L = [{'M': 7, 'N': 0, 'O': 5},{'M': 7, 'O': 15, 'N': 10, 'P': 23}] 
df = pd.DataFrame(L) 

Write the output of the following:

  1. print(df)
  2. List the index of the DataFrame df
Ans. 1

      M    N    O      P
0     7    0     5     NaN
1     7    10   15     23.0

2. (0,1)

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

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP

Pandas Dataframe Questions Class 12 IP


Share with others

Leave a Reply

error: Content is protected !!