Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create analyze.py #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions data/analyze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pandas as pd
import os

# Function to read CSV file with error handling
def read_csv_with_error_handling(file_path):
try:
return pd.read_csv(file_path)
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
return None

# File paths
file_paths = [
'data/daily_sales_data_0.csv',
'data/daily_sales_data_1.csv',
'data/daily_sales_data_2.csv'
]

# Load the three CSV files
sales_data = [read_csv_with_error_handling(file_path) for file_path in file_paths]

# Check if any file failed to load
if any(data is None for data in sales_data):
print("Error: One or more files failed to load. Exiting.")
exit()

# Combine the data from the three files
combined_sales_data = pd.concat(sales_data)

# Filter rows where the product is Pink Morsels
pink_morsels_data = combined_sales_data[combined_sales_data['product'] == 'Pink Morsels']

# Calculate total sales by multiplying quantity and price
pink_morsels_data['sales'] = pink_morsels_data['quantity'] * pink_morsels_data['price']

# Select and rearrange
formatted_data = pink_morsels_data[['sales', 'date', 'region']]

# Save the formatted data into a single output file
formatted_data.to_csv('formatted_sales_data.csv', index=False)