利用python进行数据分析-08-第六章 数据加载、存储与文件格式
1、读写文本格式的数据
pandas提供了一些用于将表格型数据读取为DataFrame对象的函数。
文件导入,使用read_csv将数据导入一个DataFrame
df= pd.read_csv('B:/test/ch06/ex1.csv')
df
Out[142]:
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,只不过需要制定分隔符
df = pd.read_table('B:/test/ch06/ex1.csv',sep=',')
df
Out[144]:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
read_csv/read_table函数的参与。
逐块读取文本文件-----nrows进行指定行数。
pd.read_csv('B:\\test\ch06\ex6.csv',nrows = 5)
Out[12]:
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
利用DataFrame的to_csv方法,我们可以将数据写入一个以都分隔的文件中:
data.to_csv(sys.statout,sep = ',')
ha