跳过坏数据
在本练习中,您将使用 read_csv() 的参数来处理含有坏数据的文件,例如记录中的数值多于列数。默认情况下,尝试导入此类文件会触发特定错误:pandas.errors.ParserError。
这里的佛蒙特州税务数据有些行已损坏。为了只加载正常的行,我们需要告诉 pandas 跳过错误。我们还希望在跳过某一行时,pandas 能发出警告,便于了解数据问题的范围。
pandas 已以 pd 导入。练习代码将尝试读取文件。如果出现 pandas.errors.ParserError,将运行 except 代码块中的内容。
本练习是课程的一部分
使用 pandas 高效导入数据
交互式实操练习
通过完成这段示例代码来试试这个练习。
try:
# Import the CSV without any keyword arguments
data = ____
# View first 5 records
print(data.head())
except pd.errors.ParserError:
print("Your data contained rows that could not be parsed.")