只加载部分工作表
面向人工阅读的电子表格往往包含多张表。例如,小型企业可能在同一工作表里,用多个表格记录不同产品类型的库存。即使是表格化数据,也可能在顶部包含元数据表头,如本题的 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)