開始使用免費開始

刪除重複值

移除重複值是取得正確計數的基本功,因為你通常不希望同一個項目被重複計算。在這個練習中,你會用 sales 中的唯一值建立一些新的 DataFrame。

sales 已可使用,且已將 pandaspd 匯入。

本練習屬於課程

使用 pandas 進行資料操作

檢視課程

練習說明

  • salesstoretype 成對重複的列移除,將結果儲存為 store_types,並印出其前幾列。
  • salesstoredepartment 成對重複的列移除,將結果儲存為 store_depts,並印出其前幾列。
  • 使用 is_holiday 欄位篩選出是假期週的列,並且對 date 去除重複,將結果儲存為 holiday_dates
  • 選取 holiday_datesdate 欄位並印出。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Drop duplicate store/type combinations
store_types = ____
print(store_types.head())

# Drop duplicate store/department combinations
store_depts = ____
print(store_depts.head())

# Subset the rows where is_holiday is True and drop duplicate dates
holiday_dates = sales[sales[____]].drop_duplicates(____)

# Print date col of holiday_dates
print(____)
編輯並執行程式碼