要读取没有标题的 CSV 文件,我们可以在方法中使用标题read_csv()。
初始化变量file_path,即 CSV 文件路径。
使用read_csv方法获取带有制表符分隔符和标题的 DataFrame。
打印带有标题的 DataFrame。
使用read_csv方法获取带有制表符分隔符且不带标题的 DataFrame。要在没有标题的情况下读取,请使用header=0。
打印没有标题的 DataFrame。
import pandas as pd file_path = 'test.csv' // 带标题 df = pd.read_csv(file_path, sep='\t', names=['x', 'y', 'z']) print "With headers, the DataFrame is: \n", df // 没有标题 df = pd.read_csv(file_path, sep='\t', header=0, names=['x', 'y', 'z']) print "Without headers, the DataFrame is: \n", df
CSV 文件“ test.csv ”包含以下数据
x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0输出结果
With headers, the DataFrame is: x y z NaN x y z 0.0 5 4 4 1.0 2 1 1 2.0 1 5 5 3.0 9 10 0 Without headers, the DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0