From cfe8be26e0d0b06dfd388d1ef4002e152cd137d9 Mon Sep 17 00:00:00 2001 From: leonhuang Date: Wed, 31 May 2023 18:27:06 -0500 Subject: [PATCH] Homework3 --- Leon_Submissions/Homework3.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Leon_Submissions/Homework3.py diff --git a/Leon_Submissions/Homework3.py b/Leon_Submissions/Homework3.py new file mode 100644 index 0000000..044634d --- /dev/null +++ b/Leon_Submissions/Homework3.py @@ -0,0 +1,31 @@ +# Question 8: It assumes that the CSV file being read +# (tomato_sales_error.csv) has a column named "Tomato_Sales." +# If the column doesn't exist in the CSV file or if there is an issue +# with the file itself, an error will be raised. + +# Question 9: +import pandas as pd + +def read_tomato_sales(file_name): + data = pd.read_csv(file_name) + + # Ensure required columns exist + required_columns = ["Store", "Region", "Tomato_Sales"] + missing_columns = set(required_columns) - set(data.columns) + if missing_columns: + raise ValueError(f"Invalid CSV format: Missing columns: {', '.join(missing_columns)}") + + # Validate Tomato_Sales column + tomato_sales = data["Tomato_Sales"] + if not tomato_sales.dtype == int and not tomato_sales.dtype == float: + raise ValueError("Invalid CSV format: 'Tomato_Sales' column must contain numerical values.") + + # Calculate total tomato sales + total_sales = tomato_sales.sum() + print(f"Total tomato sales: {total_sales}") + +# Question 10: Bytes represent raw binary data, while strings represent +# textual data. Using bytes allows us to handle various character +# encodings. By working with bytes, we can ensure that the data remains in +# its raw encoded form, allowing for accurate handling of different +# encodings. \ No newline at end of file