試算表入門

使用 pandas 的精實資料導入

Amany Mahfouz

Instructor

試算表

  • 也稱為 Excel 檔案
  • 以表格式儲存,儲存格依列與欄排列
  • 不同於平面檔,可含格式與公式
  • 一個活頁簿可含多個工作表
使用 pandas 的精實資料導入

載入試算表

  • 試算表在 pandas 中有專用載入函式:read_excel()

FreeCodeCamp 新進開發者問卷資料在試算表程式中的截圖

使用 pandas 的精實資料導入

載入試算表

import pandas as pd

# Read the Excel file
survey_data = pd.read_excel("fcc_survey.xlsx")

# View the first 5 lines of data print(survey_data.head())
    Age  AttendedBootcamp       ...                    SchoolMajor  StudentDebtOwe
0  28.0               0.0       ...                            NaN           20000
1  22.0               0.0       ...                            NaN             NaN
2  19.0               0.0       ...                            NaN             NaN
3  26.0               0.0       ...        Cinematography And Film            7000
4  20.0               0.0       ...                            NaN             NaN

[5 rows x 98 columns]
使用 pandas 的精實資料導入

載入指定欄與列

含中繼資料標頭列的問卷試算表

含多個小型資料表的發票試算表

使用 pandas 的精實資料導入

載入指定欄與列

  • read_excel()read_csv() 有許多相同關鍵字參數
    • nrows:限制讀取列數
    • skiprows:指定要略過的列數或列號
    • usecols:依欄名、位置編號或字母選欄(如「A:P」)
使用 pandas 的精實資料導入

載入指定欄與列

使用 pandas 的精實資料導入

載入指定欄與列

# Read columns W-AB and AR of file, skipping metadata header
survey_data = pd.read_excel("fcc_survey_with_headers.xlsx",
                            skiprows=2,
                            usecols="W:AB, AR")

# View data print(survey_data.head())
   CommuteTime            CountryCitizen  ...    EmploymentFieldOther    EmploymentStatus   Income
0         35.0  United States of America  ...                     NaN  Employed for wages  32000.0
1         90.0  United States of America  ...                     NaN  Employed for wages  15000.0
2         45.0  United States of America  ...                     NaN  Employed for wages  48000.0
3         45.0  United States of America  ...                     NaN  Employed for wages  43000.0
4         10.0  United States of America  ...                     NaN  Employed for wages   6000.0

[5 rows x 7 columns]
使用 pandas 的精實資料導入

一起來練習吧!

使用 pandas 的精實資料導入

Preparing Video For Download...