0% found this document useful (0 votes)
120 views17 pages

Creating DataFrames from Dictionaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views17 pages

Creating DataFrames from Dictionaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DataFrame (1 Mark Question with Answers)

1. In order to draw charts in Python, which of the following statement will be


used:
(A) import [Link] as pl (B) import [Link] as
plt
(C) Import [Link] as plt (D) import pyplot from
matplotlib as plt

2. We can create dataframe from:


(A) Series (B) Numpy arrays
(C) List of Dictionaries (D) All of the above

3. Fill in the blanks: The command used to give a heading to a graph using
matplot is
(A) [Link]() (B) [Link]() (C) [Link]() (D) [Link]()

4. In Pandas which of the following dataframe attribute can be used to know the
number of
rows and columns in a dataframe
(A) size (B) index (C) count (D) shape

5. Identify the function used for reading data from a csv file.
(A) [Link]() (B) read_csv() (C) read_data() (D)
csv_read()

6. When a DataFrame is created from List of Dictionaries, then dictionary keys


will become
(A) Column labels (B) Row labels (C) Both of the above (D) None of
the above

7. Which of the following is not a valid char type?


(A) Lineplot (B) bargraph (C) histogram (D)
statistical

8. A DataFrame object is a collection of _________ type of data.


(A) Homogenous (B) Heterogenous (C) Hybrid (D) None of the
above

9. Which of the following statement is wrong in context of DataFrame?


(A) Two dimensional size is Mutable
(B) Can perform Arithmetic operators on rows and columns.
(C) Homogeneous tabular data structure
(D) Create Dataframe from numpy ndarray

10. Which attribute is not used with Dataframe?


(A) size (B) column (C) empty (D) type

11. Which of the following can be used to specify the data while creating a
DataFrame?
(A) Series (B) List of Dictionaries (C) Structured ndarray (D) All
of these

12. Which of the following can be used to specify the data while creating a
DataFrame?
(A) Dictionaries (B) Series (C) ndarrays (D) All of the
above

13. Which of the following Python statements is used to import data from a CSV
file into
a Pandas DataFrame (Note: pd is an alias for pandas)?
(A) pd.open_csv('[Link]') (B) pd.read_csv('[Link]')
(C) pd.load_csv('[Link]') (D) pd.import_csv('[Link]')

14. Fill in the Blank Boolean indexing in Pandas DataFrame can be used for
_______.
(A) Creating a new DataFrame (B) Sorting data based on index
labels
(C) Joining data using labels (D) Filtering data based on
condition

15. Which Matplotlib plot is best suited to represent changes in data over time?
(A) Bar plot (B) Histogram (C) Line plot (D) Histogram & Bar
plot

16. Which of the following Python statements can be used to select a column
column_name
from a DataFrame df ?
(A) [Link]('column_name') (B) df['column_name']
(C) [Link]('column_name') (D) df(column_name)

17. By default, the plot() function of Matplotlib draws a ______ plot.


(A) histogram (B) column (C) bar (D) line
2 Marks Questions with Answers
1. Carefully observe the following code:
import pandas as pd
xiic = {‘amit’:34, ‘kajal’:27, ‘ramesh’:37}
xiid = {‘kajal’:34, ‘lalta’:33, ‘prakash’:38}
result = {‘PT1’:xiic, ‘PT2’:xiid}
df = [Link](result)
print(df)
Answer the following:
i) List the index of the dataframe df
ii) Find the output of the following code :
print([Link][‘kajal’:’ramesh’])
2. Consider the following DataFrame, DF

Write commands to :
i. Add a new column ‘Stream’ to the Dataframe with values (Science,
Commerce, Arts, Science.)
ii. Add a new row with values ( 5 , Mridula ,X, F , 9.8, Science)
3. Carefully observe the following code:
import pandas as pd L=[['S101','Anushree',65],['S102','Anubha',56],
['S104','Vishnu',67],['S105','Kritika',45]]
df=[Link] (L, columns=['ID','Name','Marks'])
print(df)
i. What is the shape of the data frame df?
ii. Name the index and column names of dataframe df
4. Carefully observe the following code:
import pandas as pd
D1={'S1': 'India', 'S2': 'Russia', 'S3': 'World'}
D2={'School': 'EOIS', 'Place': 'Moscow'}
data={1:D1,2:D2}
df=[Link](data)
print(df)

Answer the following


i. List the index of the DataFrame df
ii. List the column names of DataFrame df

5. Carefully observe the following code:


import pandas as pd
Y1={'Qtr1':500,'Qtr2':600,'Qtr3':120,'Qtr4': 1800}
Y2={'A' :130,'B':160,'C':150} totSales={1:Y1,2:Y2}
df=[Link](totSales)
print(df)

Answer the following:


i. List the index of the DataFrame df
ii. List the column names of DataFrame df.

6. Carefully observe the following code:


import pandas as pd
data = [{'a': 10, 'b': 20},{'a': 6, 'b': 32, 'c': 22}]
df1 = [Link](data)
print(df1)
Answer the following:
i. List the index of the DataFrame df1
ii. List the column names of DataFrame df1.
Ans:

7. Consider the code given below and answer the following question:
Ld=[{'a',10,'b':20},{'a':5,'b':10,'c':20}]
df=[Link](Ld)
print(df)

i) Write the missing import statement in the above code.


ii) How many columns will be there in the dataframe.

8. Write the code to add new column in given dataFrame DF

9. Observe the following code and write statements for the below given
questions:
import pandas as pd
player1={‘IG1’:34,’IG2’:0,’IG3’:23}
player2={‘IG1’:21,’IG2’:10,’IG3’:39}
pl={‘p1’:player1,’p2’:player2}
df=[Link](pl)
print(df)
a) Display data of player scored more than 30 runs
b) Display all rows of first inning of all players

10. Carefully observe the following code:


import pandas as pd
Year1={'Q1':5000,'Q2':8000,'Q3':12000,'Q4': 18000}
Year2={'A' :13000,'B':14000,'C':12000}
totSales={1:Year1,2:Year2}
df=[Link](totSales)
print(df)
Answer the following:
i. List the index of the DataFrame df
ii. List the column names of DataFrame df.

11. Sneha is writing a Python program to create a DataFrame using a list of


dictionaries.
However, her code contains some mistakes. Identify the errors, rewrite the
correct code,
and underline the corrections made.
import Pandas as pd
D1 = {'Name': 'Rakshit', 'Age': 25}
D2 = {'Name': 'Paul', 'Age': 30}
D3 = {'Name': 'Ayesha", 'Age': 28}
data = [D1,D2,D3)
df = [Link](data)
print(df)
12. Complete the given Python code to get the required output (ignore the
dtype attribute)
as Output:
Tamil Nadu Chennai
Uttar Pradesh Lucknow
Manipur Imphal
Code:
import _______ as pd
data = ['Chennai','_______','Imphal']
indx = ['Tamil Nadu','Uttar Pradesh','Manipur']
s = [Link](_______, indx)
print(_______)

3 Marks Question with Answers


1. Write a Python code to create a DataFrame with appropriate column
headings from the list given below:
[[1001,'IND-AUS',’2022-10-17’], [1002,'IND-PAK',’2022-10-23’],
[1003,'IND-SA' , ‘2022-10-30], [1004,'IND-NZ',’2022-11-18’]]

2. Consider the given DataFrame ‘Items’:


Write suitable Python statements for the following:
i) Add a column called Sale_Price which is 10% decreased value of
Price
ii) Add a new item named “Printer” having price 8000 and Quantity as
10.
iii) Remove the column Quantity

3. Consider the following DataFrame “HOSPITAL”


City Hospitals schools
0 Delhi 189 7916
1 Mumbai 208 8508
2 Kolkata 149 7226
3 Chennai 157 7617
Write python pandas code to create the above dataframe HOSPITAL.

4. DataFrame ‘STU_DF’:
rollno name marks
0 115 Pavni 97.5
1 236 Rishi 98.0
2 307 Preet 98.5
3 422 Paul 98.0
Perform the following operations on the DataFrame stuDF:
i. Add a new row in dataframe STU_DF with values [444,’karan’,88.0]
ii. Print no of rows and columns in dataframe STU_DF
iii. Delete row for rollno 307.

5. Write a Python code to create a DataFrame ‘Df’ using dictionary of lists for
the following data.
Arnab Ramit Samridhi
Maths 90 92 89
Science 91 81 91
Hindi 97 96 88
Ans:
import pandas as pd
D={'Arnab':[90,91,97],'Ramit':[92,81,96],'Samridhi':[89,91,88] }
Df=[Link](D,index=['Maths','Science','Hindi'])
print(Df)

6. Consider the following dataframe ndf as shown below :


Col1 Col2 Col3 Res
T1 62.893165 100.0 60.00 True
T2 94.734483 100.0 59.22 True
T3 49.090140 100.0 46.04 False
T4 38.487265 85.4 58.60 False
What will be the output produced by following statements :-
a. print( [Link] [ : , ’Col3’ : ] )
b. print( [Link][2 : , : 3] )
c. print( [Link] [ 1:3 , 2:3 ])
Ans:
7. Write a Python code to create a DataFrame Toppers with appropriate column
headings from the list given below:
[[501,'Aromal',’Commerce’],[502,'Greeshma',’Science’],[503,''
Preeti”,’Humanities’],
[504,' Rupin ',’Arts’]]

8. Consider the given DataFrame ‘Market’:


Name S_Price
0 Apple 220
1 Banana 45
2 Orange 160
Write suitable Python statements for the following:
i. Add a new item named ‘Guava' having price 175.
ii. Add a column called Margin with the following data: [80,13,50,30].
iii. Remove the column Margin.

9. Write a Python code to create a DataFrame with appropriate column


headings from the list given below: [[‘P101’,’COMPUTER’,50000],
[‘P222’,’TABLE’,5000],[P201’,’MOUSE’,1000]]

10. Consider the given DataFrame ‘Student’:


Name Percent
0 Naina 75.5
1 Rehana 82.6
2 Karina 62.8
3 Sandeep 55.4
Write suitable Python statements for the following:
i. Add a column called grade with the following data:
[‘B1’,’A2’,’C2’,’D1’].
ii. Add a new Student named ‘Krishna' having Percent 80.5.
iii. Remove the column grade.

11. Write python code to create the DataFrame employee using dictionary:
NAME SALARY
101 RAHUL 50000
102 SAKSHI 46000
103 SAKSHAM 38000
104 ARUN 25700
12. Consider the following DataFramed f and answer questions.

Write suitable Python statements for the following.


i. Change the population in Kolkata as 500000
ii. Rename the column population as “pop”
iii. Display hospitals in Delhi.

13. Write a python code to create a DataFrame with appropriate column


headings from the list given below: [[201,’Gurmeet’,95],[202,’Praveen’,89],
[203,’Suman’,97],[204.’Yogesh’,91]]

14. Consider the given DataFrame ‘Fees’:


Cname Fee
0 XII 3500
1 XI 3000
2 X 2700
3 VII 1800
Write suitable Python statements for the following:
i. Add a column called ‘Section’ with the following data:
[‘A’,’B’,’C’,’D’].
ii. Add a new Class Name named ‘IX' having price 1800.
iii. Remove the column ‘Section’.

15. Write a Python code to create a DataFrame with specified column


headings and data:

16. Consider the given DataFrame ‘df’:


Name Price
0 CHESS 150
1 CARROM BOARD 900
2 LUDO 100
3 FOOTBALL 700
Write suitable Python statements for the following:
i. Add a column called DISCOUNT with the following data:
[15,90,10,70].
ii. Add a row with the values BADMINTON 200 20
iii. Remove the column DISCOUNT.

17. Write python code to create a dataframe by using following data:


[[201,’Manoj’,4500],[202,’Dhara’,3200],[203,’Mohini’,2300]]
i. Dataframe name should be cust
ii. Use column headings as: cust_id,cust_name and amount

18. Consider the following dataframe ‘tempurature:


City max
0 Ahmedabad 31
1 Surat 29
2 Vapi 25
3 Vadodara 32
Write suitable python code to:
a) Add new column min with these data: [25,22,19,28]
b) Add new city Mahesana with 30 value
c) Delete column min
19. Write a Python code to create a DataFrame with appropriate column
headings from the list given below:
[[101,'Gurman',98],[102,'Rajveer',95],[103,'Samar' ,96],
[104,'Yuvraj',88]]

20. Consider the given DataFrame ‘Stock’:


Name Price
0 Nancy Drew 150
1 Hardy boys 180
2 Diary of a wimpy kid 225
3 Harry Potter 500
Write suitable Python statements for the following:
i. Add a column called Special_Price with the following data:
[135,150,200,440].
ii. Add a new book named ‘The Secret' having price 800.
iii. Remove the column Special_Price.
21. Write a Python program to create the following DataFrame using a list of
dictionaries.
Product Price
0 Laptop 60000
1 Desktop 45000
2 Monitor 15000
3 Tablet 30000
22. Write a Python Program to create a Pandas Series as shown below using a
dictionary.
Note that the left column indicates the indices and the right column displays
the data.
Russia Moscow
Hungary Budapest
Switzerland Bern

ASSERTION AND REASONING


1. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A): DataFrame has both a row and column index.
Reasoning (R): .loc() is a label based data selecting method to select a
specific row(s) or
column(s) which we want to select.

2. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A): - When DataFrame is created by using Dictionary, keys of
dictionary are set as
columns of DataFrame.
Reasoning (R):- Boolean Indexing helps us to select the data from the
DataFrames using a
boolean vector.

3. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):- While creating a dataframe with a nested or 2D dictionary,
Python interprets the outer dict keys as the columns and the inner keys as the
row indices.
Reasoning (R):- A column can be deleted using remove command.

4. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):-A series object can be created by calling the Series() method.
Reasoning (R): - A Series is a two-dimensional labelled data structure.

5. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A): drop() function removes data from a Dataframe temporarily.
Reasoning (R): Axis parameter is compulsory with drop() function.

6. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A): - Series is a one-dimensional labeled array capable of holding
data of any type.
Reasoning (R):- Series has both row and column values.

7. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion(A): In python pandas at attribute is to select or access multiple values
from data frame.
Reasoning(R): In python pandas, loc attribute is used to select or access a
single/multiple value(s) from dataframe.
8. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):- A series object is size mutable.
Reasoning (R): - A data frame is value mutable.

9. ASSERTION AND REASONING based questions. Mark the correct choice as


(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):- We cannot control the edge color of the bar, line style and line
width.
Reasoning (R): - To make a histogram, the data is sorted into “bins” and
number of data points in each bin is counted.

10. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A) : Pandas is an open source Python library which offers high
performance, easy-to-use data structures and data analysis tools.
Reason (R) : Professionals and developers are using the pandas library in data
science and machine learning.

11. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):- DataFrame has both a row and column index.
Reasoning (R): - A DataFrame is a two-dimensional labelled data structure like a
table of MySQL.

12. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A):- To display the first four elements of a Series object, you may
write S[:4].
Reasoning (R): - To display the first five rows of a Series object S, you may use
tail() function.

13. ASSERTION AND REASONING based questions. Mark the correct choice as
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
Assertion (A): We can add a new column in an existing DataFrame.
Reason (R): DataFrames are size mutable.
14. Assertion (A). To use the Pandas library in a Python program, one must
import it.
Reasoning (R). The only alias name that can be used with the Pandas library
is pd.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True

15. Assertion. A series is a 1D data structure which is value-mutable but size-


immutable.
Reason. Every time you change the size of a series object, change does not
take place in
the existing series object, rather a new series object is created with the new
size.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
16. Assertion. A dataframe is a 2D data structure which is value mutable and
size mutable.
Reason. Every change in a dataframe internally creates a new dataframe
object.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
17. Assertion. A dataframe is value mutable and size-mutable.
Reason. All changes occur in-place in a dataframe.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True

18. Assertion. A series object stores values of homogeneous types.


Reason. Even if values appear to be of different types, internally they are
stored in a common
datatype.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True

19. Assertion. Arithmetic operations on two series objects take place on


matching indexes.
Reason. Non-matching indexes are removed from the result of arithmetic
operation on series
objects.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
20. Assertion. Arithmetic operations on two series objects take place on
matching indexes.
Reason. For non-matching indexes of series objects in an arithmetic
operation, NaN is returned.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
21. Assertion. While changing the values of a column in a dataframe, if the
column does not
exist, an error occurs.
Reason. If values are provided for a non-existing column in a dataframe, a new
column is added
with those values.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True
22. Assertion. .loc() is a label based data selecting method to select a specific
row(s) or column(s)
which we want to select.
Reason. .iloc() can not be used with default indices if customized indices are
provided.
(A) Both A and R are true and R is the correct explanation for A
(B) Both A and R are true and R is not the correct explanation for A
(C) A is True but R is False
(D) A is false but R is True

You might also like