Python pandas IO读取保存

Pandas IO读取文件

pandas官网IO Tools

  1. read_csv,读取带分隔符的数据,默认逗号’,’
  2. read_table,读取带分隔符的数据,默认制表符’\t’
  3. read_fwf,读取没有分隔符的数据,固定列宽(想想Excel的分列功能)
  4. read_cliboard,读取剪贴板的数据

1.文件路径

  • pd.read_csv(‘f:/test/demo.csv’),注意‘/’
  • pd.read_csv(r’f\test\demo.csv’),注意‘\’前加r
  • pd.read_table('f/:test/demo.csv, sep = ‘,’)

2.read_csv分析

# 读取不同编码格式的文件
* pd.read_csv('demo_gbk.csv',encoding = 'gbk')
* pd.read_csv('demo_utf8.csv,encoding = 'utf8')
# 不读取标题(或无标题的文件)
pd.read_csv('f:/test/demo.csv',header = None)
# 读取时自定义列标题
pd.read_csv('f:/test/demo.csv',names = ['a','b','c','d'])
# 读取时,指定特定列为索引
pd.read_csv('f:/test/demo.csv',names = ['a','b','c','d'],index_col = 'd')
# 读取时,指定多列为索引(多重索引)
pd.read_csv('f:/test/demo.csv',names = ['a','b','c','d'],index_col = ['c','d'])
# 读取指定行数(如下,10行)
pd.read_csv('f:/test/demo.csv',nrows = 10)

3.用“正则表达式”读取

pd.read_table('f:/test/demo1.csv',sep = '\s+')

保存

1. df.to_csv('测试1.csv')
2. df.to_html('测试2.html'3. df.to_excel('测试3.xlsx')

详见pandas官网IO Tools

你可能感兴趣的:(Learning,Day,基础,201810)