Homework3
This commit is contained in:
@@ -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.
|
||||
Reference in New Issue
Block a user