如果有CSV文件需要跳过3行并读取标题。 使用以下代码有什么区别:
pd.read_csv('example.csv', skiprows = 3, header = 1)
pd.read_csv('example.csv', header = 4)
答案 0 :(得分:0)
演示:
源文件:
#
#
#
A,B,C
-0.8221405906050608,0.36567801665104493,-0.379336722063627
1.0601665897050705,1.1430601510100493,2.0085115463969614
0.4071960452282494,-0.10165523669413325,0.9265436970363733
-0.3085655056815222,0.5438044940307988,-2.1299177432207785
1.1371707144127696,1.7668090514496102,0.7607193776554232
不同的方法:
skiprows = 3:
In [65]: pd.read_csv(fn, skiprows=3)
Out[65]:
A B C
0 -0.822141 0.365678 -0.379337
1 1.060167 1.143060 2.008512
2 0.407196 -0.101655 0.926544
3 -0.308566 0.543804 -2.129918
4 1.137171 1.766809 0.760719
相同(显式)版本:
In [69]: pd.read_csv(fn, skiprows=3, header=0)
Out[69]:
A B C
0 -0.822141 0.365678 -0.379337
1 1.060167 1.143060 2.008512
2 0.407196 -0.101655 0.926544
3 -0.308566 0.543804 -2.129918
4 1.137171 1.766809 0.760719
skiprows = 3,header = 1:
In [70]: pd.read_csv(fn, skiprows=3, header=1)
Out[70]:
-0.8221405906050608 0.36567801665104493 -0.379336722063627
0 1.060167 1.143060 2.008512
1 0.407196 -0.101655 0.926544
2 -0.308566 0.543804 -2.129918
3 1.137171 1.766809 0.760719
头= 4:
In [71]: pd.read_csv(fn, header=4)
Out[71]:
-0.8221405906050608 0.36567801665104493 -0.379336722063627
0 1.060167 1.143060 2.008512
1 0.407196 -0.101655 0.926544
2 -0.308566 0.543804 -2.129918
3 1.137171 1.766809 0.760719
头= [4]:
In [72]: pd.read_csv(fn, header=[4])
Out[72]:
-0.8221405906050608 0.36567801665104493 -0.379336722063627
0 1.060167 1.143060 2.008512
1 0.407196 -0.101655 0.926544
2 -0.308566 0.543804 -2.129918
3 1.137171 1.766809 0.760719