1、pandas的解析函数
函数 |
描述 |
read_csv |
从文件、URL或文件对象读取分隔好的数据,, 是默认的分隔符 |
read_table |
从文件、URL或文件型对象读取分隔好的数据,制表符 (\t )是默认的分隔符 |
read_fwf |
从特定宽度格式的文件中读取数据 (无分隔符) |
read_clipboard |
read_table的剪贴板版本,在将表格从Web 页面上转换成数据时有用 |
read_excel |
从Excel的XLS或XLSX文件中读取表格数据 |
read_hdf |
读取用pandas存储的HDF5文件 |
read_html |
从HTML文件中读取所有表格数据 |
read_json |
从JSON(JavaScript Object Notation) 字符串中读取数据 |
read_msgpack |
读取MessagePack 二进制格式的pandas数据 |
read_pickle |
读取以Pythonpickle格式存储的任意对象 |
read_sas |
读取存储在SAS系统中定制存储格式的SAS数据集 |
read_sql |
将SOL查询的结果(使用SOLAlchemy)读取为pandas的DataFrame |
read_stata |
读取Stata格式的数据集 |
read feather |
读取Feather二进制格式 |
2、read_csv
、read_table
函数参数
参数 |
描述 |
path |
表明文件系统位置的字符串、URL 或文件型对象 |
sep |
用于分隔每行字段的字符序列或正则表达式 |
header |
用作列名的行号,默认是0(第一行),如果没有列名的话,应该为None |
index_col |
用作结果中行索引的列号或列名,可以是一个单一的名称/数字也可以是一个分层索引 |
names |
结果的列名列表,和 header=None 一起用 |
skiprows |
从文件开头处起,需要跳过的行数或行号列表 |
na_values |
需要用NA替换的值序列 |
comment |
在行结尾处分隔注释的字符 |
parse_dates |
尝试将数据解析为 datetime,默认是 False。如果为 True,将尝试解析所有的列。也可以指定列号或列名列表来进行解析。如果列表的元素是元组或列表,将会把多个列组合在一起进行解析(例如日期/时间将拆分为两列) |
keep date col |
如果连接列到解析日期上,保留被连接的列,默认是 False |
converters |
包含列名称映射到函数的字典 (例如[foo’:f会把函数f应用到’foo’列) |
dayfirst |
解析非明确日期时,按照国际格式处理(例如7/6/2012-> June7,2012),默认为 False |
3、分隔符(sep
)
pd.read_csv(Path('../源代码/examples/ex1.csv'))
"""
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_table(Path('../源代码/examples/ex1.csv'))
"""
a,b,c,d,message
0 1,2,3,4,hello
1 5,6,7,8,world
2 9,10,11,12,foo
"""
-------------------------------------------------------------
pd.read_table(Path('../源代码/examples/ex1.csv'),sep=',')
"""
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex3.txt'))
"""
A B C
0 aaa -0.264438 -1.026059 -0.619500
1 bbb 0.927272 0.302904 -0.032399
2 ccc -0.264273 -0.386314 -0.217601
3 ddd -0.871858 -0.348382 1.100491
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex3.txt'),sep='\s+')
"""
A B C
aaa -0.264438 -1.026059 -0.619500
bbb 0.927272 0.302904 -0.032399
ccc -0.264273 -0.386314 -0.217601
ddd -0.871858 -0.348382 1.100491
"""
4、设置列名(header
、names
)
pd.read_csv(Path('../源代码/examples/ex2.csv'))
"""
1 2 3 4 hello
0 5 6 7 8 world
1 9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex2.csv'),header=None)
"""
0 1 2 3 4
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex2.csv'),names=['a','b','c','d','message'])
"""
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex2.csv'),names=['a','b','c','d','message'],index_col='message')
"""
a b c d
message
hello 1 2 3 4
world 5 6 7 8
foo 9 10 11 12
"""
5、跳行(skiprows
)
pd.read_csv(Path('../源代码/examples/ex4.csv'))
"""
# hey!
a b c d message
# just wanted to make things for you NaN NaN NaN NaN
# who reads CSV files with computers anyway NaN NaN NaN
1 2 3 4 hello
5 6 7 8 world
9 10 11 12 foo
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex4.csv'),skiprows=[0,2,3])
"""
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
"""
6、处理缺失值(na_values
)
pd.read_csv(Path('../源代码/examples/ex5.csv'))
"""
something a b c d message
0 one 1 2 3.0 4 NaN
1 two 5 6 NaN 8 world
2 three 9 10 11.0 12 foo
"""
-------------------------------------------------------------
pd.isnull(pd.read_csv(Path('../源代码/examples/ex5.csv')))
"""
something a b c d message
0 False False False False False True
1 False False False True False False
2 False False False False False False
"""
-------------------------------------------------------------
pd.read_csv(Path('../源代码/examples/ex5.csv'),na_values={'something':['one']})
"""
something a b c d message
0 NaN 1 2 3.0 4 NaN
1 two 5 6 NaN 8 world
2 three 9 10 11.0 12 foo
"""