Here’s a worksheet focused on creating DataFrames in Python, specifically tailored for Class 12 IP:
Class 12 IP Worksheet: Creating DataFrames
Name:
Class:
Roll Number:
Date:
Section A: Multiple Choice Questions (MCQs)
Which of the following is the correct way to import the pandas library in Python?
- a)
import pandas
- b)
import pandas as pd
- c)
import pandas as p
- d)
import pd as pandas
- a)
- a)
pd.DataFrame()
- b)
pd.Data()
- c)
pd.Frame()
- d)
pd.CreateDataFrame()
- a)
What will the following code return?
import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 24]} df = pd.DataFrame(data) print(df.shape)
- a)
(2, 2)
- b)
(2, 1)
- c)
(1, 2)
- d)
(4, 2)
- a)
How can you display the first 5 rows of a DataFrame?
- a)
df.head()
- b)
df.tail(5)
- c)
df.show(5)
- d)
df.display(5)
- a)
Section B: Short Answer Questions
What is a DataFrame in pandas? Explain its structure.
Write a Python code to create a DataFrame from the following dictionary:
data = { 'Student': ['Alice', 'Bob', 'Charlie'], 'Marks': [85, 92, 78]
How can you add a new column to an existing DataFrame? Provide an example.
Explain how you can select a specific column from a DataFrame.
Answer: You can select a specific column from a DataFrame by using the column name in square brackets. For example,df['Student']
will return the 'Student' column from the DataFrame.
Section C: Long Answer Questions
Create a DataFrame from a list of dictionaries and demonstrate how to perform the following operations:
- a) Display the first 3 rows of the DataFrame.
- b) Add a new column.
- c) Delete an existing column.
Given the following DataFrame, write the code to perform the following operations:
data = { 'Product': ['Laptop', 'Tablet', 'Smartphone'], 'Price': [1000, 500, 800], 'Stock': [50, 100, 75]
- a) Select and print only the 'Product' and 'Price' columns.
- b) Filter and display rows where 'Price' is greater than 600.
- c) Update the 'Stock' of 'Tablet' to 120.
Section D: Practical Application
Write a Python program to create a DataFrame from a CSV file. Perform the following operations:
- a) Display the first 10 rows of the DataFrame.
Create a DataFrame for student records with columns: 'Name', 'Math', 'Science', 'English'. Calculate the average marks for each student and add it as a new column.
0 Comments
Please do note create link post in comment section