EX Exercises

๐Ÿ“š In-Class Tasks (20 Min): Working with Books CSV

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: Load Data and Print with .values

  1. Import the pandas library.
  2. Load the CSV file using pd.read_csv() from the URL.
    Assign custom column names "Name", "Autor", "Jahr", and "Genre" by using the names parameter together with header=None.
  3. Print the column names.
  4. Print all values of the DataFrame using .values.
  5. Print the value from the second row.

๐Ÿ” Task 2: Loop and Search for Author with iterrows()

Note: From this task on, use the original column names from the CSV file.

  1. Load the CSV file using pd.read_csv() from the URL.
  2. Loop through all rows using df.iterrows().
  3. Use a direct comparison to check if the "Author" is equal to a specific name (e.g. "George Orwell").
  4. If found, print the row index and the book title.

๐Ÿ” Task 3: Change the Year and Export the Data

  1. Loop through all rows using df.iterrows().
  2. Search for the book titled โ€œThe Alchemistโ€ by checking row[โ€œTitleโ€].
  3. If the book is found, change the value in the "Year" column to 1990.
  4. Print the updated row to verify that the change was successful.
  5. Export the updated DataFrame to a new CSV file using df.to_csv("filename.csv", index=False).
    โ†’ You only need to write the filename (e.g. "books_modified.csv"), without a full path.
    โ†’ The CSV file will be saved in the same folder where your Python file is located.
    โ†’ If you are using Visual Studio Code, you will find the file in the current working directory (the folder you see in the file explorer on the left side).

Important note: โ†’ When using iterrows(), each row is only a copy (a Series). โ†’ Changing row[โ€œYearโ€] alone does not modify the original DataFrame. โ†’ To update the actual DataFrame, you should use df.loc or create a new DataFrame with the changes.

Updated: