-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrders_Analysis.py
More file actions
77 lines (64 loc) · 2.09 KB
/
Copy pathOrders_Analysis.py
File metadata and controls
77 lines (64 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# pip install kaggle
import kaggle
import os
import zipfile
from dotenv import load_dotenv
from kaggle.api.kaggle_api_extended import KaggleApi
import pandas as pd
import sqlalchemy as sql
# Load the API KEY
env_file_path = 'requirements.env'
load_dotenv(dotenv_path=env_file_path)
api_key = os.getenv("KAGGLE_API_TOKEN")
# Or Set the Env Varibale in CLI
# Download the Kaggle Dataset
# 1. Authenticate API
api = KaggleApi()
api.authenticate()
# 2. Define paths
dataset = 'ankitbansal06/retail-orders'
download_path = './'
zip_file_path = os.path.join(download_path, 'retail-orders.zip')
try:
# Downloads the dataset zip file into the current directory
api.dataset_download_files(dataset, path=download_path)
print("Download complete!")
except Exception as e:
print(f"An error occurred: {e}")
# Unzip the file
zip_file_path = './retail-orders.zip'
download_path = './'
try:
print("Extracting files...")
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(download_path)
print("Extraction complete!")
except Exception as e:
print(f"An error occurred: {e}")
# Transform the CSV data
# Change Unknown data to NaN
df = pd.read_csv('orders.csv', na_values = ['Not Available', 'unknown'])
print(df)
print(df['Ship Mode'].unique())
# Rename Columns
print(df.columns)
df.columns = df.columns.str.lower()
df.columns = df.columns.str.replace(' ', '_')
print(df.columns)
# Find the discount, sale_price and profit
df['discount']=df['list_price']*df['discount_percent']*.01
df['sale_price']= df['list_price']-df['discount']
df['profit']=df['sale_price']-df['cost_price']
print(df)
# Drop unnessasary columns
df.drop(columns=['list_price','cost_price','discount_percent'],inplace=True)
print(df)
# Convert order_date to dateTime type
print(df.dtypes)
df['order_date'] = pd.to_datetime(df['order_date'], format='%Y-%m-%d')
print(df)
print(df.dtypes)
# Loading the resulting data into SQL
engine = sql.create_engine('mssql://DESKTOP-139CR3F/master?driver=ODBC+DRIVER+17+FOR+SQL+SERVER')
conn=engine.connect()
df.to_sql('df_orders', con = conn, index = False, if_exists = 'replace')