使用 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(____)