0% found this document useful (0 votes)
83 views6 pages

Python Pandas DataFrame Operations Guide

The document consists of a series of questions and tasks related to creating and manipulating Pandas DataFrames in Python. It covers various operations such as selecting rows and columns, importing and exporting data, and modifying DataFrames. The content is structured as a quiz or assignment for learning purposes.

Uploaded by

jyothyrajeev946
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)
83 views6 pages

Python Pandas DataFrame Operations Guide

The document consists of a series of questions and tasks related to creating and manipulating Pandas DataFrames in Python. It covers various operations such as selecting rows and columns, importing and exporting data, and modifying DataFrames. The content is structured as a quiz or assignment for learning purposes.

Uploaded by

jyothyrajeev946
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

DATAFRAME

1. Which of the following Python statements creates an empty Pandas DataFrame (Note: pd is an alias for
pandas)?
2. Which of the following can be used to specify the data while creating a DataFrame?
i. Series ii. List of Dictionaries iii. Structured ndarray iv. All of these
3. Which of the following Python commands selects the first 3 rows of a DataFrame df, assuming that labelled
index are consecutive integers starting from 0? Loc[:2]
4. Which of the following Python statements can be used to select a column column_name from a
DataFrame df ?
5. 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)?
6. In a Pandas DataFrame, if the tail() function is used without specifying the optional argument indicating
the number of rows to display, what is the default number of rows displayed, considering the DataFrame
has 10 entries?
7. 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
8. Which of the following functions is used to delete a row from a DataFrame ?
(A) drop() (B) remove_row() (C) delete() (D) pop_attribute()
9. _____________ is the type of indexing that allows us to filter a DataFrame by applying a condition. (Label
indexing / Boolean indexing)
10. Which of the following command is used to display first three rows of a DataFrame 'DF' ?
(A) [Link]() (B) [Link]() (C) [Link](3) (D) [Link](3)
11. How can we add a new column named 'name' to a DataFrame dF ? 1 (Note : Assume the values for the
column are stored in a list named data) (A) dF.add_column(name,data) (B) dF[name]=data
(C) dF.set_column(name,data) (D) [Link](name,data)
12. Which of the following Python statements is used to write a Pandas DataFrame df to a CSV file?
13. Which method is used to store data from a Pandas DataFrame dF into a CSV file ? 1 (A) dF.store_csv()
(B) dF.csv_store() (C) dF.to_csv() (D) dF.set_csv()
14. CSV stands for: i. Column Separated Value ii. Class Separated Value iii. Comma Separated Value iv. Comma
Segregated Value
15. …..

16. Assertion (A):. The output of print(df) and print([Link][:]) will be same for a DataFrame df.
Reason (R): The statement print([Link][:]) will display all rows and columns of the DataFrame df, thus
showing the entire data.
Both A and R are True, and R correctly explains A.
17. Assertion (A): We can add a new column in an existing DataFrame. Reason (R): DataFrames are size
mutable.
(A). Both Assertion (A) and Reason (R) are true, and Reason (R) is the correct explanation of Assertion (A).
18. 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.
Both A and R are true and R is the correct explanation for A
19. Write the output of the following code: import pandas as pd states = [Link](['Maharashtra', 'Gujarat',
'Kerala']) capitals = [Link](['Mumbai', 'Gandhinagar', 'Thiruvananthapuram']) data = {'State': states,
'Capital': capitals} df = [Link](data) [Link](index=1, inplace=True) print(df)
20. Write a Python program to create the following DataFrame using a dictionaries OF LIST.
21. 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]]
22. Create a DataFrame in Python from the given list:
[[‘Divya’,’HR’,95000],[‘Mamta’,’Marketing’,97000],[‘Payal’,’IT’,980000], [‘Deepak’,’Sales’,79000]] Also give
appropriate column headings as shown below:

23. Kabir, a data analyst, has stored the voter's name and age in a dictionary. Now, Kabir wants to create a list
of dictionaries to store data of multiple voters. He also wants to create a DataFrame from the same list of
dictionaries having appropriate row labels as shown below :

24. Shobit needs to create the following two series named ‘ENG’ AND ‘MATHS’.Help him to create a dataframe
from the given series eng and maths

25. Write a Python Program to create a DataFrame using a list of dictionaries with the following data :

26. The Python code written below has syntactical errors. Rewrite the correct code and underline the
correction(s) made.
import Pandas as pd
countries=[{'country';'INDIA','capital':'New Delhi'}, {'country':'USA','capital':'New York'},
{'country':'JAPAN','capital':'Tokyo'}
df=[Link](country)
print(df)
27. 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)
28. 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= [Link](df)
Print(df)
29. Predict the output
import pandas as pd
df=[Link](['Apple','Banana','Orange','Grapes','Guava'])
print(df[Link])
30. 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:

A. Predict the output of the following python statement: i. [Link] ii. df[2:4]
B. Write Python statement to display the data of Topper column of indexes CO2 to CO4.
[Link] Python statement to compute and display the difference of data of Tot_students column and
First_Runnerup column of the above given DataFrame.
31. Carefully observe the following code:
import pandas as pd
dic={'pid': [101, 102, 103, 104, 105], 'pname': ['Shyam', 'Roushan', 'Archit', 'Medha', 'Lalit'], 'sports':
['Cricket', 'Tennis', 'Football', 'Cricket', 'Cricket'], 'points': [45000, 20000, 15000,53000, 60000])
player=[Link] (dic)
print (player)
Write Python statements for the following:
i. In the dataframe player created above, set the row labels as 'Player1', 'Player2', 'Player3', 'Player4', 'Player5'.
ii. Rename the column 'points' to 'netpoint' in the DataFrame player.
32. Consider the given DataFrame ‘Stock’:
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.
33. Carefully observe the following code:

import pandas as pd Answer the following:


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

34. Consider the given DataFrame ‘Genre’:


Write suitable Python statements for the following:
i. Add a column called Num_Copies with the
following data: [300,290,450,760].
[Link] a new genre of type ‘Folk Tale' having code
as “FT” and 600 number of copies.
iii. Rename the column ‘Code’ to ‘Book_Code’.
35. 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:
[Link] the output of the following python
statement: a. print([Link]) 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.
[Link] Python statement to export the
DataFrame to a CSV file named [Link] stored
at D: drive.
36. Carefully observe the following code :
import pandas as pd Write Python statements for the following :
product={'prodid':[Link]([1,2,3,4,5]), (i) Display the names of products.
'pname':[Link](['pen', 'pencil', 'eraser', (ii) .rename the column price to newprice
'color', 'sharpener']),
'qty':[Link]([2,10,10,30,10]),
'price':[Link]([300,20,50,40,15])}
stock=[Link](product)
print(stock)
37. Consider the given DataFrame ‘result’

38. ………..

39. Consider the df Health

40. Consider the Dataframe mdf


(i) To display the records of the students having
roll numbers 2 and 3.
(ii) To increase the marks of subject Math by 4,
for all students.
(iii) Write Python statement to display the Rollno
and Name of all students who secured less than
10 marks in Maths.
(iv.)Write Python statement to display the total
marks i.e., sum of marks secured in English,
Hindi and Maths for all students.
41. Consider the df ‘password’

42. Ms. Ritika conducted an online assessment and stored the details in a DataFrame result as given below:
(i) Predict the output of the following Python statement:
print ([Link] [:, 'Attempts'] > 1)
(ii) Write the Python statement to display the last three
records.
(iii) Write Python statement to display records of 'a' and 'd'
row labels.
(iii) Write suitable Python statement to retrieve the data
stored in the file, '[Link]'into a DataFrame, 'regis'.
43. Consider the following

44. Ms. Shambhavi, a data analyst working on a college admission project, has created the following
DataFrame Sub_Details to store subjectwise details :
i.)Write suitable Python command to display the row
having index value 3.
ii) Predict the output of the following Python statement :
print(Sub_Details.loc[2:3,'Total Students'])
iii) Write suitable Python statement to display the list of
various subjects along with their corresponding seat
availability.
iv) Write suitable Python statement to display the list of
various subjects along with their corresponding seat
availability.
45. Consider the dataframe Cricket
Write suitable Python statements to perform the following tasks :
(i) Add a new column Rating to the DataFrame having the
following values : 3, 1, 2, 4, 5
(ii) Change the row labels from A, B, C, D, E to Team A, Team B,
Team C, Team D and Team E.
(iii)Change the column label of first column from 'Won' to
'Matches won'.
46. Python programmer has been given the following tasks : (i) Create two series – one to store various
product names and the other to store the corresponding price. Each series should have appropriate row
label as given below :
(ii) Create a dictionary containing ‘Product_name’ and
‘Product_price’ as keys. Add the series created in part
(i) as their corresponding values. (iii) Create a
DataFrame from the above created dictionary of series.
Help her in writing the Python program to accomplish
the above mentioned tasks.
47. Arjun is trying to load data from a CSV file, where values are separated by commas, into a DataFrame using
Pandas, but his code has errors. Identify the mistakes, rewrite the correct code, and underline the
corrections made.
import Pandas as pd
df = pd.load_csv('[Link]', sep="–")
print(data_frame)
48. Consider the DataFrame dF shown below :
49.

I. Print the last three rows of the


DataFrame df.
II. Add a new column named "Experience"
with values [5, 8, 10, 6, 7].
III. Delete the column "Salary" from the
DataFrame.
IV. Rename the column "Department" to
"Dept".
V. Display only the "Name" and "Salary"
columns from the DataFrame
50. Consider the dataframe df
[Link] the first two rows of the DataFrame df.
[Link] titles of all the movies.
[Link] the column rating.
IVDisplay the data of the 'Title' column from
indexes 2 to 4 (both included)
[Link] the column name 'Title' to 'Name'.

You might also like