DataFrame Revision
DataFrame Revision
DataFrame
1. What will be the output of the Python program men oned below ?
import pandas as pd
df=pd.DataFrame(['Apple','Banana','Orange','Grapes','Guava'])
print(df[2:4:2])
2. A Dataframe object is value mutable.
3. Which of the following func on is used to create DataFrame?
4. We can add a new row to a DataFrame using the _____________ method
5. Which of the following is used to give user defined column index in DataFrame?
6. When we create DataFrame from List of Dic onaries, then number of columns in
DataFrame is equal to the _______
7. The following code create a dataframe named ‘D1’ with ___________ columns.
import pandas as pd
dicts = [{‘a’:10, ‘b’:20}, {‘a’:5, ‘b’:10, ‘c’:20}]
D1 = pd.DataFrame(dicts)
8. The following code create a dataframe named ‘D1’ with _______________ columns.
import pandas as pd
D1 = pd.DataFrame([1,2,3] )
9. The following code create a dataframe named ‘D1’ with ______ rows.
import pandas as pd
LoD = [{'a':10, 'b':20}, {'a':5, 'b':10, 'c':20}]
D1 = pd.DataFrame(LoD)
10.In DataFrame, by default new column added as the _____________ column
11.When we create DataFrame from List of Dic onaries, then dic onary keys will become.
12. Which of the following statements is false?
(a) Dataframe is size mutable
(b) Dataframe is value mutable
(c) Dataframe is immutable
(d) Dataframe is capable of holding mul ple type of data
13. In the following statement, if column ‘Rollno’ already exists in the DataFrame ‘D1’ then
the assignment statement will __________
D1['Rollno'] = [1,2,3] #There are only three rows in DataFrame D1
16. In given code dataframe ‘D1’ has _____ rows and ______ columns.
import pandas as pd
LoD = {“Name” : [“Amit”, “Anil”,”Ravi”], “RollNo” : [1,2,3]}
D1 = pd.DataFrame(LoD)
17. Name the func on that returns number of non-NaN values of dataframe ?
19. D1[ : ] = 77 , will set __________ values of a Data Frame ‘D1’ to 77.
22. To delete a column from a DataFrame, you may use _____ statement.(drop is a method del is statement)
23.
24. Write a statement to append the DataFrame ‘DF2’ to the DataFrame ‘DF’
25. Write a statement to display the sales made by all sales persons in the year 2017.
26. Write a statement to add new column for another year ‘2018’ with values 55000, 65000, 75000, 85000, 95000
(a) DF[2018] = 55000, 65000, 75000, 85000, 95000
(b) DF[2018] = [55000, 65000, 75000, 85000, 95000]
(c) DF[2018] = (55000, 65000, 75000, 85000, 95000)
(d) All of the above
27. Write a statement to add new row for ‘Raman’ with values 55000, 66000, 77000, 88000
(a) DF.loc[‘Raman’] = 55000, 66000, 77000, 88000
(b) DF.loc[‘Raman’] = [55000, 66000, 77000, 88000]
(c) Both of the above
(d) None of the above
28. Raman was caught in the case of chea ng so his Boss decided to set his sales of all years to 0(Zero). Help him to write
the code for same.
(a) DF.loc[‘Raman’] = {0} (b) DF.loc[‘Raman’] = [0] (c) DF.loc[‘Raman’] = 0 (d) All of the above
29. Write a statement to delete a column having column label as 2017.
30. Write a statement to delete two columns having column label as 2017 and 2016.
31. Replace the column label from 2016 to 2020.
32. Consider the DataFrame ‘DF’ given below and answer the ques ons from Q to Q.
Following DataFrame ‘DF’ containing marks of five students in three subjects.
43.
44. Mr. Ankit wants to change the index of the Data Frame and the output for the same is given
below. Iden fy the correct statement to change the index
45. He wants to arrange records of all the passenger’s year wise in descending order.
(a) df.sort_values(by=’Year’, ascending=True, inplace =False)
(b) df.sort_values(by=’Year’, ascending=False, inplace =False)
(c) df.sort_values(by='Year', ascending=False, inplace =True)
(d) df.sort_values(by=’Year’, descending=True, inplace =True)
46.
51. For the above created Dataframe df in Q.50 write single line statements for each of the
following parts (a) to (c), which use Pandas method:
a. To display the 'Names' and 'Marks' columns from the DataFrame.
b. To change the 'Marks' in the 4th row (i.e. for index 3) to 91.5
c. To display the rows where number of 'Trials' in the examina on is less than 2 and
'Marks' is greater than 95
52. Consider the following DataFarme df and answer ques ons from (i) to (v) :
i) Write down the command that will give the following output :
rollno 2
Name Pranshu
Age 14
Marks 60.9
Class 12B
Name: 20, dtype: object
ii) The teacher wants to know the highest marks secured by the students. Which statement would help her to get the
correct answer ?
iii) Which of the following statement(s) will add new column ‘fee’ as third column with values
[3200,3400,4500,3100,3200,4000] in DataFrame df ?
iv) Which of the following commands is useds to remove the column ‘Age’ in the DataFrame df?
ii) Write the statement to mul ply two series ‘A’ and ‘B’.