A Assignment

๐Ÿ“š Working with Books CSV

Please save your solutions for Exercises 1 to 3 in a single Python script named unit07__ex(1-3)code.py.
For Bonus Exercise, use a separate script named unit07__ex4code.py.

Save both scripts in the same unit07 folder, compress the folder into a .zip file, and upload it to ILIAS.

For more information, please visit the following link:
https://geomoer.github.io/moer-base-python/unit00/unit00-04_submission_guidelines.html

Make sure your code is clearly structured and includes comments where helpful.

Introduction

Use the online CSV file and follow the steps to explore the dataset using pandas.

๐Ÿ“ฅ CSV URL:
https://geomoer.github.io/moer-base-python/assets/tests/unit07/books.csv


๐Ÿ  Task 1: Count Books in the Genre โ€œFantasyโ€

  1. Filter the DataFrame to find all books where the "Genre" is "Fantasy".
  2. Count how many such books there are.
  3. Print the number.

๐Ÿ  Task 2: List All Books Before the Year 1950

  1. Filter the DataFrame to include only books with a "Year" before 1950.
  2. Print the titles and years of those books.

๐Ÿ’ก Hint: Donโ€™t forget to convert "Year" to int before comparing.


๐Ÿ  Task 3: Print All Unique Genres

  1. Print a list of all unique values in the "Genre" column.
  2. Then, print how many different genres there are in total.

๐ŸŒŸ Task 4: Mark Old Books with apply()

  1. Use the .apply() method with axis=1 to create a new column called "Old".
  2. If the "Year" of a book is before 1950, set โ€œOldโ€ to โ€œyesโ€ or โ€œnoโ€
  3. Export the new DataFrame with the "Old" columns.

๐Ÿ’ก Hint: Inside your function, make sure to convert "Year" to an integer before comparing.


๐ŸงŠ Task 5 (Bonis):Titanic Dataset โ€” Beginner Exercises (Python & Pandas)

These exercises are designed to help you practice basic data analysis with the Titanic dataset using Pandas. Use the dataset from this URL:

https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv

Load it with:

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")

โœ… Exll passengers younger than 10

Filter the dataset and show all passengers whose age is below 10.

Hint: Use a boolean condition on the Age column.


โœ… Eount men and women

Find out:

  • how many male passengers are in the dataset
  • how many female passengers are in the dataset

Hint: Use value_counts().


โœ… Everage age per ticket class

Calculate the average age for each passenger class (Pclass).

Which class had the highest average age?


โœ… Eurvival rate by gender

Compute the survival rate for:

  • male passengers
  • female passengers

Hint: groupby("Sex")["Survived"].mean().


โœ… Eassengers whose name contains โ€œSmithโ€

Show all rows where the passengerโ€™s name contains the string "Smith".

Hint: Use .str.contains("Smith") on the Name column.


Good luck! ๐Ÿšขโœจ

Updated: