pd.read_csv的header用法

默认Header = 0:

In [3]: import pandas as pd

In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_dataset\t_user.csv')

In [5]: t_user.head()
Out[5]: 
     uid  age  sex active_date     limit
0  26308   30    1  2016-02-16  5.974677
1  78209   40    1  2016-02-21  5.292154
2  51930   35    1  2016-04-19  6.292055
3  10113   25    1  2016-03-12  6.292055
4  17067   35    1  2016-02-16  5.974677

header=0,表示第一行为标题行

In [6]: t_user2 = pd.read_csv(r'C:\Users\Song\Desktop\jdd_dataset\t_user.csv',header = 0)

In [7]: t_user2.head()
Out[7]: 
     uid  age  sex active_date     limit
0  26308   30    1  2016-02-16  5.974677
1  78209   40    1  2016-02-21  5.292154
2  51930   35    1  2016-04-19  6.292055
3  10113   25    1  2016-03-12  6.292055
4  17067   35    1  2016-02-16  5.974677

header=None时,即指明原始文件数据没有列索引,这样read_csv会自动加上列索引,除非你给定列索引的名字。

In [9]: t_user3 = pd.read_csv(r'C:\Users\Song\Desktop\jdd_dataset\t_user.csv',header = None)

In [10]: t_user3.head()
Out[10]: 
       0    1    2            3             4
0    uid  age  sex  active_date         limit
1  26308   30   01   2016-02-16  5.9746772897
2  78209   40   01   2016-02-21  5.2921539288
3  51930   35   01   2016-04-19  6.2920545271
4  10113   25   01   2016-03-12  6.2920545271

更多参考:

  1. https://blog.csdn.net/ly_ysys629/article/details/55107237
  2. https://www.cnblogs.com/datablog/p/6127000.html

posted on 2018-04-18 11:10  星辰之衍  阅读(7868)  评论(0编辑  收藏

导航