EX | Exercises |
📚 In-Class Tasks: Working with Books CSV
Use the file book_list.csv
and follow the steps below in a Jupyter notebook or Python script.
✅ Task 1: Import and Print All Rows
- Import the
pandas
library. - Load the CSV file using
pd.read_csv()
. - Print all rows of the DataFrame.
import pandas as pd
df = pd.read_csv("book_list.csv")
print(df)
🔍 Task 2: Search for a Book by Author and Print It
- Use a condition to find all books written by George Orwell.
- Print only the rows that match.
print(df[df["Author"] == "George Orwell"])
✏️ Task 3: Replace the Author and Save New File
- Change the author’s name “George Orwell” to “Orwell, George”.
- Print the updated row to verify.
- Save the modified DataFrame to a new CSV file.
df.loc[df["Author"] == "George Orwell", "Author"] = "Orwell, George"
print(df[df["Author"] == "Orwell, George"])
df.to_csv("book_list_modified.csv", index=False)
Make sure to check your folder for the newly saved file book_list_modified.csv
.