第6章 数据加载、存储与文件格式(Part1)
目录
csv文件读入
pandas中的解析函数:
read_csv读入DataFrame
#code
file ='data/examples/ex1.csv'
df = pd.read_csv(file)
print(df)
#output
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
read_table读入DataFrame
#需要指定分隔符
df = pd.read_table(file, sep = ',')
read_csv函数参数
header参数
读入如下的csv文件:
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
不修改header参数(默认header = 0),则表示第一行为标题行
#code
file ='data/examples/ex2.csv'
df = pd.read_csv(file)
print(df)
#output
1 2 3 4 hello
0 5 6 7 8 world
1 9 10 11 12 foo
header = None,表示原文件数据没有标题行。在没有指定列索引的情况下,read_csv会自动加上列索引
#code
file ='data/examples/ex2.csv'
df = pd.read_csv(file, header = None)
print(df)
#output
0 1 2 3 4
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
names参数
自定义列索引
#code
file ='data/examples/ex2.csv'
names = ['a', 'b', 'c', 'd', 'message']
df = pd.read_csv(file, names = names)
print(df)
#output
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
index_col参数
index_col参数默认值为None,会添加一列作为行索引
#code
file ='data/examples/ex2.csv'
names = ['a', 'b', 'c', 'd', 'message']
df = pd.read_csv(file, names = names, index_col = None)
print(df)
#output
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
当index_col = 0时, 则不添加行索引,选择第一列作为行索引
#code
file ='data/examples/ex2.csv'
names = ['a', 'b', 'c', 'd', 'message']
df = pd.read_csv(file, names = names, index_col = 0)
print(df)
#output
b c d message
a
1 2 3 4 hello
5 6 7 8 world
9 10 11 12 foo
若希望将DataFrame中的某一列作为行索引(例如'message'列),则使index_col = 该列列编号或列名称:
#code
file ='data/examples/ex2.csv'
names = ['a', 'b', 'c', 'd', 'message']
df = pd.read_csv(file, names = names, index_col = 'message')
print(df)
#output
a b c d
message
hello 1 2 3 4
world 5 6 7 8
foo 9 10 11 12
若希望将多个列做成层次化索引,只需要传入列编号或列名称组成的列表即可。我们读入如下csv文件:
key1,key2,value1,value2
one,a,1,2
one,b,3,4
one,c,5,6
one,d,7,8
two,a,9,10
two,b,11,12
two,c,13,14
two,d,15,16
#code
file ='data/examples/csv_mindex.csv'
df = pd.read_csv(file, index_col = ['key1', 'key2'])
print(df)
#output
value1 value2
key1 key2
one a 1 2
b 3 4
c 5 6
d 7 8
two a 9 10
b 11 12
c 13 14
d 15 16
异形文件处理
skiprows参数
选择跳过读入文件的某些行。我们读入如下csv文件:
# hey!
a,b,c,d,message
# just wanted to make things more difficult for you
# who reads CSV files with computers, anyway?
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
#code
file ='data/examples/ex4.csv'
df = pd.read_csv(file, skiprows = [0, 2, 3])
print(df)
#output
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
缺失值处理
缺失值处理是文件解析任务中重要的组成部分。缺失值要么没有(空字符串),要么用某个标记值表示。默认情况下,pandas会用一组经常出现的标记值进行识别,如NA、-1.#IND、NULL等。我们读入如下csv文件:
,something,a,b,c,d,message
0,one,1,2,3.0,4,
1,two,5,6,,8,world
2,three,9,10,11.0,12,foo
#code
file ='data/examples/ex5.csv'
df = pd.read_csv(file, header = None)
print(df)
#output
0 1 2 3 4 5 6
0 NaN something a b c d message
1 0.0 one 1 2 3.0 4 NaN
2 1.0 two 5 6 NaN 8 world
3 2.0 three 9 10 11.0 12 foo
In [251]: pd.isnull(df)
Out[251]:
0 1 2 3 4 5 6
0 True False False False False False False
1 False False False False False False True
2 False False False False True False False
3 False False False False False False False
na_values参数
na_values可以接受一组用于表示缺失值的字符串:
#code
file ='data/examples/ex5.csv'
df = pd.read_csv(file, header = None, na_values = ['three']) #原文件中'three'表示空值
print(df)
#output,发现原本three的位置被读成了NaN
0 1 2 3 4 5 6
0 NaN something a b c d message
1 0.0 one 1 2 3.0 4 NaN
2 1.0 two 5 6 NaN 8 world
3 2.0 NaN 9 10 11.0 12 foo
可以用一个字典指定不同的NA标记值:
#code
file ='data/examples/ex5.csv'
sentinels = {1: ['three'], 6: ['NaN', 'foo']}
df = pd.read_csv(file, header = None, na_values = sentinels)
print(df)
#output
#发现原数据中列1中的'three'和列6中的'NaN'、'foo'都被读成了空
0 1 2 3 4 5 6
0 NaN something a b c d message
1 0.0 one 1 2 3.0 4 NaN
2 1.0 two 5 6 NaN 8 world
3 2.0 NaN 9 10 11.0 12 NaN
逐块读取文本
在处理很大文件,或找出大文件中的参数集以便于后续处理时,我们可能只想读取文件的一小部分或逐块对文件进行迭代。
one two three four key
0 0.467976 -0.038649 -0.295344 -1.824726 L
1 -0.358893 1.404453 0.704965 -0.200638 B
2 -0.501840 0.659254 -0.421691 -0.057688 G
3 0.204886 1.074134 1.388361 -0.982404 R
4 0.354628 -0.133116 0.283763 -0.837063 Q
... ... ... ... ..
9995 2.311896 -0.417070 -1.409599 -0.515821 L
9996 -0.479893 -0.650419 0.745152 -0.646038 E
9997 0.523331 0.787112 0.486066 1.093156 K
9998 -0.362559 0.598894 -1.843201 0.887292 G
9999 -0.096376 -1.012999 -0.657431 -0.573315 0
[10000 rows x 5 columns]
nrows参数
如果对于上面的数据只想读取几行,可以通过nrows指定:
#code
file ='data/examples/ex6.csv'
df = pd.read_csv(file, nrows = 5)
print(df)
#output
one two three four key
0 0.467976 -0.038649 -0.295344 -1.824726 L
1 -0.358893 1.404453 0.704965 -0.200638 B
2 -0.501840 0.659254 -0.421691 -0.057688 G
3 0.204886 1.074134 1.388361 -0.982404 R
4 0.354628 -0.133116 0.283763 -0.837063 Q
chunksize参数
要逐块读取文件,需要设置chunksize(行数):
#code
file ='data/examples/ex6.csv'
chunker = pd.read_csv(file, chunksize = 100)
print(chunker)
#output
<pandas.io.parsers.TextFileReader object at 0x000002B666C54400>
read_csv所返回的TextParser对象可以使你根据chunksize对文件进行逐块迭代。
这里我们统计原文件中各种key的个数,操作如下:
#code
tot = Series([])
for piece in chunker:
tot = tot.add(piece['key'].value_counts(), fill_value = 0)
tot = tot.sort_values(ascending = False)
print(tot[:10])
#output
E 368.0
X 364.0
L 346.0
O 343.0
Q 340.0
M 338.0
J 337.0
F 335.0
K 334.0
H 330.0
dtype: float64