From edaf988e64ca3d7600ca9339760048c700f77172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=B5=E5=8D=9A=E5=B7=9E?= Date: Tue, 5 May 2026 22:08:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E6=A5=AD=E7=B9=B3?= =?UTF-8?q?=E4=BA=A4-=E5=BC=B5=E5=8D=9A=E5=B7=9E-AIPE03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework/m2_pandas_cleaning.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/homework/m2_pandas_cleaning.py b/homework/m2_pandas_cleaning.py index fa36bff..fec862e 100644 --- a/homework/m2_pandas_cleaning.py +++ b/homework/m2_pandas_cleaning.py @@ -19,8 +19,7 @@ def green_read_csv(): 讀取 orders_raw.csv,回傳原始 DataFrame(不做任何清理) 提示:pd.read_csv() """ - # TODO: 你的程式碼 - pass + return pd.read_csv("datasets/ecommerce/orders_raw.csv") def green_shape(df): @@ -28,8 +27,7 @@ def green_shape(df): 回傳 DataFrame 的 (列數, 欄數) tuple 提示:df.shape """ - # TODO: 你的程式碼 - pass + return df.shape def green_dtypes(df): @@ -37,8 +35,7 @@ def green_dtypes(df): 回傳 DataFrame 的欄位型別 (Series) 提示:df.dtypes """ - # TODO: 你的程式碼 - pass + return df.dtypes # ============================================================ @@ -51,8 +48,9 @@ def yellow_clean_columns(df): 回傳清理後的 DataFrame(不要修改原始 df) 提示:df.columns.str.strip().str.lower() """ - # TODO: 你的程式碼 - pass + new_df = df.copy() + new_df.columns = new_df.columns.str.strip().str.lower() + return new_df def yellow_clean_amount(df): @@ -62,8 +60,15 @@ def yellow_clean_amount(df): 回傳清理後的 DataFrame(不要修改原始 df) 提示:.str.replace() + .astype(float) """ - # TODO: 你的程式碼 - pass + new_df = df.copy() + new_df['amount'] = ( + new_df['amount'] + .astype(str) + .str.replace('$', '', regex=False) + .str.replace(',', '', regex=False) + .astype(float) + ) + return new_df def yellow_drop_duplicates(df): @@ -71,8 +76,7 @@ def yellow_drop_duplicates(df): 移除完全重複的列,回傳去重後的 DataFrame 提示:df.drop_duplicates() """ - # TODO: 你的程式碼 - pass + return df.drop_duplicates() # ============================================================