python pandas IO tools 之read_csv文件读写参数详解

python pandas IO tools 之csv文件读写

英文原文:pandas IO tools

读取csv文件:pd.read_csv(),写入csv文件:pd.to_csv()
pandas还可以读取一下文件:
read_csv,
read_excel,
read_hdf,
read_sql,
read_json,
read_msgpack (experimental),
read_html,
read_gbq (experimental),
read_stata,
read_sas,
read_clipboard,
read_pickle;
相应的写入:
to_csv,
to_excel,
to_hdf,
to_sql,
to_json,
to_msgpack (experimental),
to_html,
to_gbq (experimental),
to_stata,
to_clipboard,
to_pickle.

常用参数的读取csv文件

import pandas as pd
obj=pd.read_csv('f:/ceshi.csv')
print obj
print type(obj)
print obj.dtypes
  Unnamed: 0  c1  c2  c3
0          a   0   5  10
1          b   1   6  11
2          c   2   7  12
3          d   3   8  13
4          e   4   9  14
<class 'pandas.core.frame.DataFrame'>
Unnamed: 0    object
c1             int64
c2             int64
c3             int64
dtype: object

ceshi.csv为有列索引没有行索引的数据,read_csv会自动加上行索引,即使原数据集有行索引。
read_csv读取的数据类型为Dataframe,obj.dtypes可以查看每列的数据类型

obj_2=pd.read_csv('f:/ceshi.csv',header=None,names=range(2,5))
print obj_2
    2   3   4
0  c1  c2  c3
1   0   5  10
2   1   6  11
3   2   7  12
4   3   8  13
5   4   9  14

header=None时,即指明原始文件数据没有列索引,这样read_csv为自动加上列索引,除非你给定列索引的名字。

obj_2=pd.read_csv('f:/ceshi.csv',header=0,names=range(2,5))
print obj_2
   2  3   4
0  0  5  10
1  1  6  11
2  2  7  12
3  3  8  13
4  4  9  14

header=0,表示文件第0行(即第一行,python,索引从0开始)为列索引,这样加names会替换原来的列索引。

obj_2=pd.read_csv('f:/ceshi.csv',index_col=0)
print obj_2
   c1  c2  c3
a   0   5  10
b   1   6  11
c   2   7  12
d   3   8  13
e   4   9  14
obj_2=pd.read_csv('f:/ceshi.csv',index_col=[0,2])
print obj_2
      c1  c3
  c2        
a 5    0  10
b 6    1  11
c 7    2  12
d 8    3  13
e 9    4  14

index_col为指定数据中那一列作为Dataframe的行索引,也可以可指定多列,形成层次索引,默认为None,即不指定行索引,这样系统会自动加上行索引(0-)

obj_2=pd.read_csv('f:/ceshi.csv',index_col=0,usecols=[0,1,2,3])
print obj_2
   c1  c2  c3
a   0   5  10
b   1   6  11
c   2   7  12
d   3   8  13
e   4   9  14
obj_2=pd.read_csv('f:/ceshi.csv',index_col=0,usecols=[1,2,3])
print obj_2
    c2  c3
c1        
0    5  10
1    6  11
2    7  12
3    8  13
4    9  14

usecols:可以指定原数据集中,所使用的列。在本例中,共有4列,当usecols=[0,1,2,3]时,即选中所有列,之后令第一列为行索引,当usecols=[1,2,3]时,即从第二列开始,之后令原始数据集的第二列为行索引。

obj_2=pd.read_csv('f:/ceshi.csv',index_col=0,nrows=3)
print obj_2
   c1  c2  c3
a   0   5  10
b   1   6  11
c   2   7  12

nrows:可以给出从原始数据集中的所读取的行数,目前只能从第一行开始到nrows行。

datetime handing 数据中日期处理

obj_3=pd.read_csv('f:/ceshi_date.csv',index_col=0,)
print obj_3
print type(obj_3.index)
                   A  B  C
date                      
20090101           a  2  3
20090102           b  3  4
20090103           c  4  5
<class 'pandas.indexes.numeric.Int64Index'>
obj_3=pd.read_csv('f:/ceshi_date.csv',index_col=0,parse_dates=True)
print obj_3
print type(obj_3.index)
                   A  B  C
date                      
2009-01-01         a  2  3
2009-01-02         b  3  4
2009-01-03         c  4  5
<class 'pandas.tseries.index.DatetimeIndex'>

parse_dates=True:可令字符串解析成时间格式。

data='date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c'
print data
date,value,cat
1/6/2000,5,a
2/6/2000,10,b
3/6/2000,15,c
from StringIO import StringIO
print pd.read_csv(StringIO(data),parse_dates=[0],index_col=0)
            value cat
date                 
2000-01-06      5   a
2000-02-06     10   b
2000-03-06     15   c
print pd.read_csv(StringIO(data),parse_dates=[0],index_col=0,dayfirst=True)
            value cat
date                 
2000-06-01      5   a
2000-06-02     10   b
2000-06-03     15   c

US常用时间格式:MM/DD/YYYY,dayfirst=True:可将其改为DD/MM/YYYY

分隔符和阈值

tem='id|level|category\npatient1|123,000|x\npatient2|23,000|y\npatient3|1,234,018|z'
print tem
id|level|category
patient1|123,000|x
patient2|23,000|y
patient3|1,234,018|z
print pd.read_csv(StringIO(tem),sep='|')
         id      level category
0  patient1    123,000        x
1  patient2     23,000        y
2  patient3  1,234,018        z
print pd.read_csv(StringIO(tem),sep='|',thousands=',')
         id    level category
0  patient1   123000        x
1  patient2    23000        y
2  patient3  1234018        z
  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨岚❤️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值