DataFrames in Python, specifically tailored for Class 12 IP: WORKSHEET NO 1

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)

  1. 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

  2. Which method is used to create a DataFrame in pandas?

    • a) pd.DataFrame()
    • b) pd.Data()
    • c) pd.Frame()
    • d) pd.CreateDataFrame()

  3. 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)

  4. 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)


Section B: Short Answer Questions

  1. What is a DataFrame in pandas? Explain its structure.

  2. Write a Python code to create a DataFrame from the following dictionary:

    data = { 'Student': ['Alice', 'Bob', 'Charlie'], 'Marks': [85, 92, 78]
    }


  3. How can you add a new column to an existing DataFrame? Provide an example.

  4. 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

  1. 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.

  2. 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]
    } df = pd.DataFrame(data)
    • 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

  1. 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.
  2. 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.

Post a Comment

0 Comments