開始使用免費開始

從多個欄位取得 datetime

有時候,datetime 資料會被拆成多個欄位。資料集可能同時有日期欄與時間欄,或是把日期拆成年、月、日三個欄位。

本次調查資料的其中一個欄位就是這樣被拆開:日期在 Part2StartDate,時間在 Part2StartTime。你的任務是使用 read_excel()parse_dates 參數,把它們合併為一個新的 datetime 欄位,並給它一個新名稱。

已將 pandaspd 匯入。

本練習屬於課程

使用 pandas 的精實資料導入

檢視課程

練習說明

  • 建立字典 datetime_cols,指定新欄位 Part2StartPart2StartDatePart2StartTime 組成。
  • 載入調查回應檔案,將該字典提供給 parse_dates 參數,以建立新的 Part2Start 欄位。
  • 使用 describe() 方法檢視新欄位 Part2Start 的摘要統計。

動手互動練習

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

# Create dict of columns to combine into new datetime column
datetime_cols = {"Part2Start": ____}


# Load file, supplying the dict to parse_dates
survey_data = pd.read_excel("fcc_survey_dts.xlsx",
                            ____)

# View summary statistics about Part2Start
print(survey_data.Part2Start.describe())
編輯並執行程式碼