开始使用免费开始使用

处理多个工作表

主要面向人工阅读(而非机器处理)的工作簿,常会把同一主题的数据分散在多个工作表中。例如,一个文件可能按业务所在的每个地区或每个年份各建一个交易记录工作表。

FreeCodeCamp New Developer Survey 文件的结构也类似:不同年份的样本回答分别放在不同的工作表里。您需要把它们汇总到同一个 dataframe 中以便分析。

已将 pandaspd 导入。所有工作表都已读入有序字典 responses,工作表名称为键、dataframe 为值,您可以通过 values() 方法获取各个 dataframe。

本练习是课程的一部分

使用 pandas 高效导入数据

查看课程

练习说明

  • 创建一个空的 dataframe,命名为 all_responses
  • 编写一个 for 循环,遍历字典 responses 的所有值。
  • 将每个 dataframe 连接到 all_responses 上,并将结果重新赋给同名变量。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create an empty dataframe
all_responses = ____

# Set up for loop to iterate through values in responses
for df in ____:
  # Print the number of rows being added
  print("Adding {} rows".format(df.shape[0]))
  # Concatenate all_responses and df, assign result
  all_responses = pd.concat(____)

# Graph employment statuses in sample
counts = all_responses.groupby("EmploymentStatus").EmploymentStatus.count()
counts.plot.barh()
plt.show()
编辑并运行代码