Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 18 additions & 7 deletions homework/m1_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
def green_mean():
"""建立 [10, 20, 30, 40, 50],回傳所有元素的平均值 (float)"""
# TODO: 你的程式碼
pass
arr = np.array([10, 20, 30, 40, 50])
return arr.mean()


def green_double():
"""建立 [10, 20, 30, 40, 50],回傳所有元素乘以 2 的 ndarray"""
# TODO: 你的程式碼
pass
arr = np.array([10, 20, 30, 40, 50])
return arr*2


def green_filter():
"""建立 [10, 20, 30, 40, 50],回傳大於 25 的元素 (ndarray)"""
# TODO: 你的程式碼
pass
arr = np.array([10, 20, 30, 40, 50])
return arr[arr > 25]


# ============================================================
Expand All @@ -42,7 +45,8 @@ def green_filter():
def yellow_expensive_count(prices):
"""回傳單價 > 1000 的商品數量 (int)"""
# TODO: 你的程式碼
pass
prices_arr = np.array(prices)
return len(prices_arr[prices_arr > 1000])


def yellow_top3_stock_indices(stocks):
Expand All @@ -51,7 +55,9 @@ def yellow_top3_stock_indices(stocks):
提示:np.argsort
"""
# TODO: 你的程式碼
pass
stocks_arr = np.array(stocks)
stocks_arr = np.argsort(stocks)[::-1]
return stocks_arr[:3]


def yellow_restock_cost(prices, stocks):
Expand All @@ -60,7 +66,8 @@ def yellow_restock_cost(prices, stocks):
提示:布林遮罩 + .sum()
"""
# TODO: 你的程式碼
pass
total = prices[prices < 500] *50
return total.sum()


# ============================================================
Expand All @@ -77,4 +84,8 @@ def red_double11_prices(prices, stocks):
提示:np.where 可以巢狀使用
"""
# TODO: 你的程式碼
pass
prices_07 = prices[stocks >= 100] * 0.7
prices_09 = prices[(stocks >= 20) & (stocks < 100)] * 0.9
prices_original = prices[stocks < 20]
final_price = np.concatenate([prices_original, prices_07, prices_09])
return final_price
39 changes: 32 additions & 7 deletions homework/m2_pandas_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def green_read_csv():
提示:pd.read_csv()
"""
# TODO: 你的程式碼
pass
DATA = "/datasets/ecommerce/orders_raw.csv"
df = pd.read_csv(DATA)
return df


def green_shape(df):
Expand All @@ -29,7 +31,8 @@ def green_shape(df):
提示:df.shape
"""
# TODO: 你的程式碼
pass

return df.shape


def green_dtypes(df):
Expand All @@ -38,7 +41,8 @@ def green_dtypes(df):
提示:df.dtypes
"""
# TODO: 你的程式碼
pass

return df.dtypes


# ============================================================
Expand All @@ -52,7 +56,9 @@ def yellow_clean_columns(df):
提示:df.columns.str.strip().str.lower()
"""
# TODO: 你的程式碼
pass

df.columns = df.columns.str.strip().str.lower()
return df


def yellow_clean_amount(df):
Expand All @@ -63,7 +69,12 @@ def yellow_clean_amount(df):
提示:.str.replace() + .astype(float)
"""
# TODO: 你的程式碼
pass
arr = (df['amount']
.astype(str)
.str.replace('$', '', regex=False)
.str.replace(',', '', regex=False)
.astype(float))
return arr


def yellow_drop_duplicates(df):
Expand All @@ -72,7 +83,9 @@ def yellow_drop_duplicates(df):
提示:df.drop_duplicates()
"""
# TODO: 你的程式碼
pass

arr = df.drop_duplicates()
return arr


# ============================================================
Expand All @@ -93,4 +106,16 @@ def red_clean_orders(path):
提示:pd.to_datetime(errors='coerce')
"""
# TODO: 你的程式碼
pass

df = pd.read_csv(path)
df.columns = df.columns.str.strip().str.lower()
df["amount"] = (df['amount']
.astype(str)
.str.replace('$', '', regex=False)
.str.replace(',', '', regex=False)
.astype(float))
df["order_date"] = pd.to_datetime(df['order_date'], errors='coerce')
df = df.dropna(subset=['order_date'])
df['qty'] = df['qty'].fillna(df['qty'].median())
df = df.drop_duplicates()
return df
82 changes: 74 additions & 8 deletions homework/m3_pandas_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,42 @@ def green_load_and_merge():
提示:pd.merge(how='left')
"""
# TODO: 你的程式碼
pass

orders = pd.read_csv("datasets/ecommerce/orders_clean.csv")
customers = pd.read_csv("datasets/ecommerce/customers.csv")
products = pd.read_csv("datasets/ecommerce/products.csv")
df = (
orders
.merge(customers, on = 'customer_id', how='left')
.merge(products, on = 'product_id', how='left')
)
return df

def green_row_count(df):
"""回傳 DataFrame 的列數 (int)"""
# TODO: 你的程式碼
pass
orders = pd.read_csv("datasets/ecommerce/orders_clean.csv")
customers = pd.read_csv("datasets/ecommerce/customers.csv")
products = pd.read_csv("datasets/ecommerce/products.csv")
df = (
orders
.merge(customers, on = 'customer_id', how='left')
.merge(products, on = 'product_id', how='left')
)
return df.shape[0]


def green_column_list(df):
"""回傳 DataFrame 的所有欄位名稱 (list)"""
# TODO: 你的程式碼
pass
orders = pd.read_csv("datasets/ecommerce/orders_clean.csv")
customers = pd.read_csv("datasets/ecommerce/customers.csv")
products = pd.read_csv("datasets/ecommerce/products.csv")
df = (
orders
.merge(customers, on = 'customer_id', how='left')
.merge(products, on = 'product_id', how='left')
)
return df.columns


# ============================================================
Expand All @@ -50,7 +73,9 @@ def yellow_top_category(df):
提示:groupby('category')['amount'].sum()
"""
# TODO: 你的程式碼
pass

amount = df.groupby('category')['amount'].sum().sort_values(ascending=False).head(1)
return amount


def yellow_gold_vip_stats(df):
Expand All @@ -60,7 +85,12 @@ def yellow_gold_vip_stats(df):
提示:df[df['vip_level'] == 'Gold']
"""
# TODO: 你的程式碼
pass

vip = df[df['vip_level'] == 'Gold']
vip_sum = len(vip['order_id'])
vip_amount = float(vip['amount'].sum())
Gold_Vip = (vip_sum, vip_amount)
return Gold_Vip


def yellow_region_avg_amount(df):
Expand All @@ -70,7 +100,9 @@ def yellow_region_avg_amount(df):
提示:groupby('region')['amount'].mean()
"""
# TODO: 你的程式碼
pass

Series = df.groupby('region')['amount'].mean()
return Series


# ============================================================
Expand All @@ -94,4 +126,38 @@ def red_rfm_top5(df):
提示:groupby('customer_id').agg(...)
"""
# TODO: 你的程式碼
pass
orders = pd.read_csv("datasets/ecommerce/orders_clean.csv")
customers = pd.read_csv("datasets/ecommerce/customers.csv")
products = pd.read_csv("datasets/ecommerce/products.csv")
# R = orders.groupby('customer_id')['order_date'].max()
# F = orders.groupby('customer_id')['order_id'].count()
# M = orders.groupby('customer_id')['amount'].sum().sort_values(ascending=False).head()
# df = (
# orders
# .merge(customers, on = 'customer_id', how='left')
# .merge(products, on = 'product_id', how='left')
# )
RFM = (
orders.groupby('customer_id')
.agg(
recency = ('order_date', 'max'),
frequency = ('order_id', 'count'),
monetary = ('amount', 'sum'),
)
.reset_index()
)
rfm_named = RFM.merge(
customers[['customer_id', 'customer_name']],
on = 'customer_id',
how='left',
)

final_5 = (
rfm_named
.sort_values("M", ascending=False)
.head(5)
.reset_index(drop=True)
[['customer_id', 'customer_name', 'R', 'F', 'M']]
)

return final_5
42 changes: 35 additions & 7 deletions homework/m4_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def green_avg_by_month():
提示:df['order_date'].dt.month
"""
# TODO: 你的程式碼
pass
df = _load_data()

result = df.groupby(df["order_date"].dt.month)['amount'].mean().round(1)

return result


def green_top3_dates():
Expand All @@ -37,7 +41,9 @@ def green_top3_dates():
提示:value_counts().head(3)
"""
# TODO: 你的程式碼
pass
df = _load_data()
top3 = df['order_date'].dt.date.value_counts().head(3)
return top3


def green_date_range():
Expand All @@ -46,7 +52,11 @@ def green_date_range():
格式為 pandas Timestamp
"""
# TODO: 你的程式碼
pass
df = _load_data()
time_1 = df['order_date'].min()
time_2 = df['order_date'].max()
result = (time_1, time_2)
return result


# ============================================================
Expand All @@ -60,7 +70,9 @@ def yellow_monthly_revenue():
提示:set_index('order_date').resample('ME')['amount'].sum()
"""
# TODO: 你的程式碼
pass
df = _load_data()
test = df.set_index('order_date').resample('ME')['amount'].sum()
return test


def yellow_rolling_avg(monthly_revenue):
Expand All @@ -71,7 +83,8 @@ def yellow_rolling_avg(monthly_revenue):
提示:.rolling(window=3).mean()
"""
# TODO: 你的程式碼
pass

return monthly_revenue.rolling(window=3).mean()


def yellow_category_median(df):
Expand All @@ -81,7 +94,8 @@ def yellow_category_median(df):
提示:groupby + median + sort_values
"""
# TODO: 你的程式碼
pass

return df.groupby('category')['amount'].median().sort_values(ascending=False)


# ============================================================
Expand All @@ -101,4 +115,18 @@ def red_monthly_report():
提示:resample + agg + pct_change
"""
# TODO: 你的程式碼
pass
df = _load_data()
df['year_mon'] = df['order_date'].dt.to_period('M')
monthly_report = (df.groupby('year_mon')
.agg(
當月訂單數 = ('order_id', 'count'),
當月總營收 = ('amount', 'sum'),
單月不重複客戶數 = ('customer_id', 'nunique'),
)
.sort_index()
)
monthly_report['客單價'] = (monthly_report['當月總營收'] / monthly_report['當月訂單數'])
monthly_report['月營收成長率'] = (monthly_report['當月總營收'].pct_change()*100)
monthly_report = monthly_report.reset_index().rename(columns={'year_mon' : '月份'})
monthly_report['月份'] = monthly_report['月份'].astype(str)
return monthly_report
Loading
Loading