只載入部分試算表
給人閱讀的試算表常會有多個表格。例如,小型企業可能在同一張工作表裡,放入不同產品類型的庫存表格。即使是看起來像表格的資料,也可能在最上方有中繼資料的標頭列,例如這份 New Developer Survey 的資料。這些中繼資料很有用,但我們不希望它們出現在 dataframe 中。你將使用 read_excel() 的 skiprows 參數,只擷取真正的資料。你也會建立一個要傳給 usecols 的字串,只讀取 AD 欄與從 AW 到 BA 的欄位,這些欄與未來工作目標有關。
已將 pandas 以 pd 匯入。
本練習屬於課程
使用 pandas 的精實資料導入
練習說明
- 建立一個字串
col_string,指定pandas應載入欄位AD以及範圍AW到BA。 - 載入
fcc_survey_headers.xlsx',設定skiprows與usecols,略過前 2 列的中繼資料,並只取得col_string中指定的欄位。 - 檢視結果 dataframe 中所選取的欄名。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create string of lettered columns to load
col_string = ____
# Load data with skiprows and usecols set
survey_responses = ____("fcc_survey_headers.xlsx",
____,
____)
# View the names of the columns selected
print(survey_responses.columns)