一.文件的读取和写入
1.普通读取
pd.read_csv, pd.read_table, pd.read_excel, 分别用来读取csv、txt、excel文件
- 常用的公共参数
header=None:第一行不作为列名
index_col:把某一列或几列作为索引
usecols:读取列的集合,默认读取所有的列
parse_dates:需要转化为时间的列
nrows:读取的数据行数。
2.详细解释
- header= None:
read_csv读取时会自动识别表头,数据有表头时不能设置header为空(默认读取第一行,即header=0);数据无表头时,若不设置header,第一行数据会被视为表头,应传入names参数设置表头名称或设置header=None。
下面我们来看例子中读取的数据。
数据是带有表头的,不设置header时,会把第一行(col1,col2,col3,col4)读入并且当成表头
当设置为header=None时,认为没有表头,就把第一行(col1,col2,col3,col4)读入并且当成一行数据。
如果没有表头,怎么传入names来设置表头呢?
pd.read_table('../data/my_table.txt'\
,header = 0\