开始使用免费开始使用

使用 pandas 将平面文件导入为 DataFrame(1)

在上一个练习中,您已经能够将包含不同数据类型列的平面文件导入为 numpy 数组。不过,pandas 中的 DataFrame 对象更适合存放此类数据。好在我们可以使用 pandas 的 read_csv()read_table() 函数,轻松将混合数据类型的文件导入为 DataFrame。

本练习是课程的一部分

Python 数据导入入门

查看课程

练习说明

  • 使用别名 pd 导入 pandas 包。
  • titanic.csv 读取为名为 df 的 DataFrame。文件名已存储在对象 file 中。
  • 使用一次 print() 调用查看该 DataFrame 的表头。

交互式实操练习

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

# Import pandas as pd
____

# Assign the filename: file
file = 'titanic.csv'

# Read the file into a DataFrame: df
df = pd.read_csv(____)

# View the head of the DataFrame
print(____)
编辑并运行代码