Assignment 2

Author

Your Name

Published

September 13, 2024

Instructions

Complete the questions by adding the code under the relevant chunks.

Please submit your assignment as html file to me via email. Please call your assignment: “yourlastname_assignment2.html”. For example, I would submit something called “popescu_assignment2.html”. Please make sure that it is all in lower case.

To obtain the same look as this template, make sure that your you assignment preamble or YAML looks like below:

---
title: "Assignment 2"
author: "Your Name"
date: "September 13, 2024"
format:
  html:
    toc: true
    number-sections: true
    colorlinks: true
    smooth-scroll: true
    embed-resources: true
---

Note that Headers such as “Question” need to be marked with # (i.e # Question will appear as header in bold and larger font after you render the document. It will thus appear in the Table of Contents). Regular text can just be written one line underneath.

reticulate::use_python("/opt/anaconda3/bin/python", required = TRUE)

1 Question

Create a list of five fruits

Replace the third fruit in the list with “cherry”.

2 Question

Add “mango” to the beginning of the list using an appropriate method.

3 Question

Remove the last fruit from the list.

4 Question

Sort the list alphabetically

5 Question

Create a tuple with the following values: 5, 10, 15, 20.

6 Question

Attempt to replace the second value with 12 and explain why this fails.

7 Question

Concatenate this tuple with the numbers tuple: ("A", "B", "C").

8 Question

Extract the last two elements from the concatenated tuple.

9 Question

Write a function greet_user(name) that takes one argument, name, and prints a personalized greeting in the format: "Hello, [name]!".

10 Question

Write a function find_max(a, b, c) that takes three numbers as arguments and returns the largest of the three. Test it on [10, 25, 15].

11 Question

Create a dictionary called students_scores with the names of three students as keys and their test scores as values: "Alice": 85, "Bob": 90, "Charlie": 78 Update the dictionary to add a new student, “David,” with a score of 92, and print the updated dictionary.

12 Question

Create two sets, my_fruits and friend_fruits, containing the names of fruits you and your friend like. The fruits you like are: “apple”, “banana”, “cherry”. The fruits that your friend likes are: “cherry”, “date”, “banana”.

Find and print:

  1. Fruits you both like (intersection).
  2. Fruits only you like (difference).

13 Question

Create a dictionary called favorite_fruits where the keys are names of people, and the values are their favorite fruits.

“Alice”: “Apple”
“Bob”: “Banana”
“Charlie”: “Cherry”

Write a script that performs the following:
1. Add a new person and their favorite fruit to the dictionary.
2. Change the favorite fruit of an existing person.
3. Print all the people and their favorite fruits in a readable format.

14 Question

Create a DataFrame from the following dictionary:

Python
data = {
    "Name": ["Alice", "Bob", "Charlie", "Diana"],
    "Age": [25, 30, None, 22],
    "City": ["New York", "Paris", "London", "Tokyo"],
    "Score": [85, 90, 78, None]
}

Perform the following:

  1. Fill the missing values in the Age column with the mean age.
  2. Replace missing values in the Score column with the value 0.
  3. Print the updated DataFrame.

15 Question

Using the following dictionary, create a DataFrame:

Python
data = {
    "Team": ["A", "A", "B", "B", "C", "C"],
    "Player": ["John", "Alice", "Bob", "Diana", "Eve", "Charlie"],
    "Score": [10, 15, 20, 25, 30, 35]
}

Perform the following:

  1. Calculate the total score for each team.
  2. Print the resulting DataFrame with teams and their total scores.

16 Question

Given the following DataFrame of city weather data:

Python
import pandas as pd
data = {
    'Date': ['2025-01-01', '2025-01-01', '2025-01-01', '2025-01-02', '2025-01-02', '2025-01-02'],
    'City': ['New York', 'Chicago', 'Miami', 'New York', 'Chicago', 'Miami'],
    'Temperature': [32, 25, 28, 30, 22, 27],
    'Humidity': [60, 65, 70, 58, 63, 68]
}

df = pd.DataFrame(data)

Pivot the DataFrame so that City becomes the column headers, and the index is Date. Include both Temperature and Humidity as values.

            Chicago_Temperature  ...  New York_Humidity
Date                             ...                   
2025-01-01                   25  ...                 60
2025-01-02                   22  ...                 58

[2 rows x 6 columns]

17 Question

Given the following dataset:

Python
import pandas as pd

data = {
    "Department": ["HR", "HR", "IT", "IT", "Sales", "Sales", "Sales"],
    "Employee": ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace"],
    "Salary": [50000, 55000, 70000, 80000, 45000, 47000, 52000]
}

df = pd.DataFrame(data)

Calculate the total salary for each department.

18 Question

Given the following dataset:

Python
import pandas as pd

data = {
    "Department": ["HR", "HR", "IT", "IT", "Sales", "Sales", "Sales"],
    "Employee": ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace"],
    "Salary": [50000, 55000, 70000, 80000, 45000, 47000, 52000]
}

df = pd.DataFrame(data)

Determine the average salary for each department.

19 Question

Given the following datasets:

Python
df_a = pd.DataFrame({
    'Employee': ['Alice', 'Bob', 'Charlie', 'Diana'],
    'Department': ['HR', 'HR', 'IT', 'IT'],
    'Age': [25, 30, 35, 40]
})

df_b = pd.DataFrame({
    'Employee': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
    'Salary': [50000, 55000, 70000, 80000, 45000]
})

Perform an inner join on the two DataFrames to include only employees present in both datasets.

20 Question

Given the following datasets:

Python
df_a = pd.DataFrame({
    'Employee': ['Alice', 'Bob', 'Charlie', 'Diana'],
    'Department': ['HR', 'HR', 'IT', 'IT'],
    'Age': [25, 30, 35, 40]
})

df_b = pd.DataFrame({
    'Employee': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
    'Salary': [50000, 55000, 70000, 80000, 45000]
})

Perform an outer join to include all employees and their respective details from both DataFrames.