CLASS 12 DATAFRAME ACCESSING DATAFRAME WORKSHEET NO 3



Short Answer Questions

  1. Adding Rows: Write a Python code snippet to add a new row to the following DataFrame:


    import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 24]} df = pd.DataFrame(data)

    Add a row with Name: 'Tom' and Age: 30.

  2. Adding Columns: Given the following DataFrame:


    import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 24]} df = pd.DataFrame(data)

    Write a Python code snippet to add a new column 'City' with values 'New York' and 'Boston'.

  3. Insert Column at Specific Position: How can you insert a new column 'Gender' after the 'Name' column in the DataFrame shown above? Write the code for it.

  4. Multiple Rows: Explain how you would add multiple rows at once to a DataFrame. Provide a code example where two rows are added with names 'Alice' and 'Bob', and ages 22 and 26 respectively.

Section C: Programming Task

  1. Create a DataFrame with columns 'StudentID', 'Name', and 'Marks'. Add the following data:

    • Row 1: 101, 'Alice', 85
    • Row 2: 102, 'Bob', 90
    • Row 3: 103, 'Charlie', 78

    Then, add a new row for a student with StudentID: 104, Name: 'David', and Marks: 88.

  2. Add a new column 'Grade' to the DataFrame created in Task 1. Assign grades based on the following criteria:

    • Marks >= 85: 'A'
    • Marks 70-84: 'B'
    • Marks < 70: 'C'

Post a Comment

0 Comments