0% found this document useful (0 votes)
25 views10 pages

DataFrame Assignment2024

The document outlines a series of assignments related to creating and manipulating DataFrames using Python's Pandas library. It includes tasks such as adding columns and rows, displaying specific data, and performing calculations. Additionally, it poses questions about assertions, code corrections, and DataFrame operations.

Uploaded by

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

DataFrame Assignment2024

The document outlines a series of assignments related to creating and manipulating DataFrames using Python's Pandas library. It includes tasks such as adding columns and rows, displaying specific data, and performing calculations. Additionally, it poses questions about assertions, code corrections, and DataFrame operations.

Uploaded by

Jagrit Chanana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Frame Assignment

Q1 Create the given DataFrame

State Area Forest


1 Assam 78438 2797
2 Delhi 1483 6
3 Kerala 38852 1663

a) Insert a new column in the DataFrame named as


“Population” with the values [5,50,20]
b) Add a new row with the values [‘Goa’, 25542, 7832,4]
c) Insert a new column named as “Area_Forest” that store
the result of expression Area / Forest.
d) Display the details of that states where population is
below 10 Lakh.
e) Display the details of the state with maximum Area
f) Display the details of the state with minimum Forest

Q2 Create the given DataFrame

Ecode Ename Salary Department


0 101 Ravi 15800 Sales
1 102 Harsh 14500 Accounts
2 103 Kashish 20800 Sales
3 104 Gauri 14200 Purchase

a) Add a new record with the following data


105,’Deepa’,17200,’Accounts’.
b) Add a new column named as hobby with values
[‘Hockey’,’Boxing’,’Dance’,’Boxing’,’Singing’]
c) Add a new column named as ‘Pfund’ and it is calculated
as 10% of Salary.
d) Add another column named as “Net” and it is equal to
the sum of Salary and Pfund.
e) Display the details of all employees of Sales Department.
f) Display the Data Type of the above DataFrame.
1
g) Remove the column named as hobby from the
DataFrame.
h) Display the record of employee with employee code 103
and 105
i) Display the maximum salary among all the employees.
j) Display the minimum salary among Sales Department
Employees.
k) Display the list of all employees Names.

Q3 Create the Given DataFrame

Manas Riya Neev Pooja


English 75 85 74 91
Maths 52 65 57 50
Science 85 74 65 60
SSt 88 57 92 90

a) Add details of a New student “Aditya” with Marks as


80,60,40,30
b) Change the marks of Riya as 80,70,85,50
c) Change the marks of Pooja in all subjects as 90
d) Add a new subject named as “Hindi” and marks of all
students are [75,95,62,55,60]
e) Add a new row with index Total and it stores the total in
all subjects of every student.
f) Remove the entry of subeject “SSt” from the DataFrame.
g) Also update the total marks after removing “Sst” subject.
h) Remove the record of “Pooja” from the DataFrame.
i) Change the Name of Subjects as “English” to “Eng”.
Science” to “Sc”.

Q4 i. Both A and R are true and R is the correct explanation for A


ii. Both A and R are true and R is not the correct explanation
for A
iii. A is True but R is False
iv. A is false but R is True

(a)
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.

(b)
2
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

Q5 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=pd.DataFrame(totSales)
print(df)
Answer the following:
i. List the index of the DataFrame df
ii. List the column names of DataFrame df.

Q6 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]]

Q7 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

Q8 Mr. Som, a data analyst has designed the DataFrame df that


contains data about Computer Olympiad with ‘CO1’, ‘CO2’,
‘CO3’, ‘CO4’, ‘CO5’ as indexes shown below. Answer the
following questions:

3
A. Predict the output of the following python statement:
i. df.shape ii. df[2:4]
B. Write Python statement to display the data of Topper
column of indexes CO2 to CO4.

C. Write Python statement to compute and display the


difference of data of Tot_students column and First_Runnerup
column of the above given DataFrame

Q9 The python code written below has syntactical errors. Rewrite


the correct code and underline the corrections made.
Import pandas as pd
df ={"Technology":["Programming","Robotics","3D
Printing"],"Time(in months)":[4,4,3]}
df= Pd.dataframe(df)
Print(df)

Q10 Create a DataFrame in Python from the given list:


[[‘Divya’,’HR’,95000],[‘Mamta’,’Marketing’,97000],[‘Payal’,’IT’,98
0000], [‘Deepak’,’Sales’,79000]]
Also give appropriate column headings as shown below:

Q11 Consider the given DataFrame ‘Genre’:


Type Code
0 Fiction F
1 Non Fiction NF
2 Drama D
3 Poetry P
Write suitable Python statements for the following:
i. Add a column called Num_Copies with the following data:
[300,290,450,760].
ii. Add a new genre of type ‘Folk Tale' having code as “FT” and
600 number of copies.
iii. Rename the column ‘Code’ to ‘Book_Code’

Q12 Ekam, a Data Analyst with a multinational brand has


designed the DataFrame df that contains the four quarter’s
sales data of different stores as shown below:

4
Answer the following questions:
i. Predict the output of the following python statement:
a. print(df.size) b. print(df[1:3])
ii. Delete the last row from the DataFrame.
iii. Write Python statement to add a new column Total_Sales
which is the addition of all the 4 quarter sales.
iv. Write Python statement to export the DataFrame to a CSV
file named data.csv stored at D: drive

Q13 Display first row of dataframe ‘DF’


a. print(DF.head(1))
b. print(DF[0 : 1])
c. print(DF.iloc[0 : 1])
d. All of the above

Q14 In the following statement, if column ‘mark’ already exists


in the DataFrame ‘Df1’ then the assignment statement
will __________ Df1['mark'] = [95,98,100] #There are only
three rows in DataFrame Df1
a. Return error
b. Replace the already existing values.
c.Add new column
d. None of the above

Q15 To delete a row, the parameter axis of function drop( ) is


assigned the value______________
a. 0 b. 1 c. 2 d. 3

Q16 Write code to delete rows those getting 5000 salary.


a. df=df.drop[salary==5000]
b. df=df[df.salary!=5000]
c. df.drop[df.salary==5000,axis=0]
d. df=df.drop[salary!=5000]

Q17 DF1.loc[ ] method is used to ______ # DF1 is a DataFrame


a. Add new row in a DataFrame ‘DF1’
b. To change the data values of a row to a particular value
c.Both of the above
d.None of the above

5
Q18 Write code to delete the row whose index value is A1 from
dataframe df.
a. df=df.drop(‘A1’)
b. df=df.drop(index=‘A1’)
c. df=df.drop(‘A1,axis=index’)
d. df=df.del(‘A1’)

Q19 What is dataframe?


a. 2 D array with heterogeneous data
b. 1 D array with homogeneous data
c. 2 D array with homogeneous data
d. 1 D array with heterogeneous data

Q20 Write the single line command to delete the column


“marks” from dataframe df using drop function.
a. df=df.drop(col=‘marks’)
b. df=df.drop(‘marks’,axis=col)
c. df=df.drop(‘marks’,axis=0)
d. df=df.drop(‘marks’,axis=1)

Q21 Difference between loc() and iloc().:


a. Both are Label indexed based functions.
b. Both are Integer position-based functions.
c. loc() is label based function and iloc() integer position based
function.
d. loc() is integer position based function and iloc() index
position based function.

Q22 Which command will be used to delete 3 and 5 rows of the


data frame. Assuming the data frame name as DF.
a. DF.drop([2,4],axis=0)
b. DF.drop([2,4],axis=1)
c. DF.drop([3,5],axis=1)
d. DF.drop([3,5])

6
Q23

Q24

7
Q25

Q26

8
Q27

Q28

Q29

9
Q30

Q31

10

You might also like