pandas小结(三)pandas数据的导入导出
pandas的数据导入导出功能,非常简单。pandas支持的导入数据格式有csv,excel,sql,json,html,pickle等。具体可以参看:https://pandas.pydata.org/pandas-docs/version/0.20/io.html 页面。
示例文件如下:
1Student ID;name;age;gender
21000;Jack;21;Male
31001;Lucy;22;Female
41002;Rose;23;Female
51003;David;24;Male
代码如下:
1#!/usr/bin/env python
2# _*_ coding: utf-8 _*_
3import pandas as pd
4import numpy as np
5# 读取csv文件, sep指定字符串的分隔符, 默认为逗号
6data = pd.read_csv('student.csv', sep = ';')
7print(data)
8data.to_html('student.html')
上面示例是生成到html文件,同样,生成到pickle文件可以使用:
1data.to_pickle('student.pickle')
2# 再读取该文件可以使用如下指令
3data = pd.read_pickle("D:\\data\\student.pickle")
4print(data)
保留原格式,生成到文本文件可以使用下面的操作:
1srdata = pd.read_excel('2017.xls',encoding='gbk')
2with open('xls.txt','w',encoding='UTF-8') as outfile:
3 srdata.to_string(outfile)
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/pandas-read-write/6149.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.