本文要点刚要:
(一)读文本文件格式的数据函数: read_csv,read_table
1. 读不同分隔符的文本文件, 用参数 sep
2. 读无字段名 (表头) 的文本文件 , 用参数 names
3. 为文本文件制定索引, 用 index_col
4. 跳行读取文本文件, 用 skiprows
5. 数据太大时需要逐块读取文本数据用 chunksize 进行分块.
(二)将数据写成文本文件格式函数: to_csv
范例如下:
(一)读取文本文件格式的数据集
1.read_csv 和 read_table 的区别:
- #read_csv 默认读取用逗号分隔符的文件, 不需要用 sep 来指定分隔符
- import pandas as pd
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.csv')
- #read_csv 如果读的是用非逗号分隔符的文件, 必须要用 sep 指定分割符, 不然读出来的是原文件的样子, 数据没被分割开
- import pandas as pd
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt')
- # 与上面的例子可以对比一下区别
- import pandas as pd
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|')
- #read_table 读取文件时必须要用 sep 来指定分隔符, 否则读出来的数据是原始文件, 没有分割开.
- import pandas as pd
- pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.csv')
- #read_table 读取数据必须指定分隔符
- import pandas as pd
- pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|')
2. 读取文本文件时不用 header 和 names 指定表头时, 默认第一行为表头
- # 用 header=None 表示数据集没有表头, 会默认用阿拉伯数字填充表头和索引
- pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|',header=None)
- # 用 names 可以自定义表头
- pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|',
- names=['x1','x2','x3','x4','x5'])
3. 默认用阿拉伯数字指定索引; 用 index_col 指定某一列作为索引
- names=['x1','x2','x3','x4','x0']
- pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|',
- names=names,index_col='x0')
4. 以下示例是用 skiprows 将 hello 对应的行跳过后读取其他行数据, 不管首行是否作为表头, 都是将表头作为第 0 行开始数
可以对比一下三个例子的区别进行理解
pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt')
- names=['x1','x2','x3','x4','x0']
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt',names=names,
- skiprows=[0,3,6])
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt',
- skiprows=[0,3,6])
- pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt',header=None,
- skiprows=[0,3,6])
5. 分块读取, data1.txt 中总共 8 行数据, 按照每块 3 行来分, 会读 3 次, 第一次 3 行, 第二次 3 行, 第三次 1 行数据进行读取.
注意这里在分块的时候跟跳行读取不同的是, 表头没作为第一行进行分块读取, 可通过一下两个例子对比进行理解.
- chunker = pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt',chunksize=3)
- for m in chunker:
- print(len(m))
- print m
- chunker = pd.read_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data1.txt',header=None,
- chunksize=3)
- for m in chunker:
- print(len(m))
- print m
(二)将数据写入文本格式用 to_csv
以 data.txt 为例, 注意写出文件时, 将索引也写入了
- data=pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|')
- print data
- # 可以用 index=False 禁止索引的写入.
- data=pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|')
- data.to_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\outdata.txt',sep='!',index=False)
- # 可以用 columns 指定写入的列
- data=pd.read_table('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\data.txt',sep='|')
- data.to_csv('C:\\Users\\xiaoxiaodexiao\\pythonlianxi\\test0424\\outdata2.txt',sep=',',index=False,
- columns=['a','c','d'])
来源: https://www.cnblogs.com/gangandimami/p/8955564.html