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
- Import the
pandas
library. - Load the CSV file using
pd.read_csv()
from the URL.
Assign custom column names"Name"
,"Autor"
,"Jahr"
, and"Genre"
by using thenames
parameter together withheader=None
. - Print the column names.
- Print all values of the DataFrame using
.values
. - 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.
- Load the CSV file using
pd.read_csv()
from the URL. - Loop through all rows using
df.iterrows()
. - Use a direct comparison to check if the
"Author"
is equal to a specific name (e.g."George Orwell"
). - If found, print the row index and the book title.
๐ Task 3: Change the Year and Export the Data
- Loop through all rows using
df.iterrows()
. - Search for the book titled โThe Alchemistโ by checking row[โTitleโ].
- If the book is found, change the value in the
"Year"
column to 1990. - Print the updated row to verify that the change was successful.
- 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.