Pandas dataframe数据写入文件和数据库
转自:http://www.dcharm.com/?p=584
Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFrame是一张多维的表,大家可以把它想象成一张Excel表单或者Sql表。之前这篇文章已经介绍了从各种数据源将原始数据载入到dataframe中,这篇文件介绍怎么将处理好的dataframe中的数据写入到文件和数据库中。
首先我们通过二维ndarray创建一个简单的DataFrame:
1
2
3
4
5
6
7
8
|
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(3, 4)) df 0 1 2 3 0 1.0492286140081302 -0.7922606407983686 0.020418054868760225 -1.6649819403741724 1 0.3485250628814134 -2.117606544377745 1.466822878437205 -0.9249205656243358 2 1.3073567907490637 -0.7350348086218035 0.2856083175408006 -0.9053483976251634 |
1. Dataframe写入到csv文件
1
|
df .to_csv( 'D:\\a.csv' , sep= ',' , header=True, index=True) |
第一个参数是说把dataframe写入到D盘下的a.csv文件中,参数sep表示字段之间用’,’分隔,header表示是否需要头部,index表示是否需要行号。
2. Dataframe写入到json文件
1
|
df .to_json( 'D:\\a.json' ) |
把dataframe写入到D盘下的a.json文件中,文件的内容为
1
|
{ "0" :{ "0" :1.049228614, "1" :0.3485250629, "2" :1.3073567907}, "1" :{ "0" :-0.7922606408, "1" :-2.1176065444, "2" :-0.7350348086}, "2" :{ "0" :0.0204180549, "1" :1.4668228784, "2" :0.2856083175}, "3" :{ "0" :-1.6649819404, "1" :-0.9249205656, "2" :-0.9053483976}} |
3.Dataframe写入到html文件
1
|
df .to_html( 'D:\\a.html' ) |
把dataframe写入到D盘下的a.html文件中,文件的内容为
1
|
< table border = "1" class = "dataframe" >\n < thead >\n < tr style = "text-align: right;" >\n < th ></ th >\n < th >0</ th >\n < th >1</ th >\n < th >2</ th >\n < th >3</ th >\n </ tr >\n </ thead >\n < tbody >\n < tr >\n < th >0</ th >\n < td >1.049229</ td >\n < td >-0.792261</ td >\n < td >0.020418</ td >\n < td >-1.664982</ td >\n </ tr >\n < tr >\n < th >1</ th >\n < td >0.348525</ td >\n < td >-2.117607</ td >\n < td >1.466823</ td >\n < td >-0.924921</ td >\n </ tr >\n < tr >\n < th >2</ th >\n < td >1.307357</ td >\n < td >-0.735035</ td >\n < td >0.285608</ td >\n < td >-0.905348</ td >\n </ tr >\n </ tbody >\n</ table > |
在浏览器中打开a.html的样式为
4.Dataframe写入到剪贴板中
这个是我认为最为贴心的功能, 一行代码可以将dataframe的内容导入到剪切板中,然后可以复制到任意地方
1
|
df .to_clipboard() |
5.Dataframe写入到数据库中
1
|
df .to_sql( 'tableName' , con=dbcon, flavor= 'mysql' ) |
第一个参数是要写入表的名字,第二参数是sqlarchmy的数据库链接对象,第三个参数表示数据库的类型,“mysql”表示数据库的类型为mysql。
Pandas dataframe数据写入文件和数据库的更多相关文章
- POI解析excel,将批量数据写入文件或数据库
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- 将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy
将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy import pandas as pd from sqlalchemy import create_engine ...
- Pandas 把数据写入csv
Pandas 把数据写入csv from sklearn import datasets import pandas as pd iris = datasets.load_iris() iris_X ...
- spark 将dataframe数据写入Hive分区表
从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API.Da ...
- pandas-19 DataFrame读取写入文件的方法
pandas-19 DataFrame读取写入文件的方法 DataFrame有非常丰富的IO方法,比如DataFrame读写csv文件excel文件等等,操作很简单.下面在代码中标记出来一些常用的读写 ...
- 学习springMVC框架配置遇到的问题-数据写入不进数据库时的处理办法
配置完了,运行,数据写入不到数据库中,就应该想UserAction 中的handleRequest()方法有没有进去,然后就设置断点.如果发现程序没有进去,就再想办法进去.
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):2 H264数据写入文件
本文档为iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述续篇,主要描述: CMSampleBufferRef读取实际数据 序列参数集(Sequence Para ...
- Pandas DataFrame数据的增、删、改、查
Pandas DataFrame数据的增.删.改.查 https://blog.csdn.net/zhangchuang601/article/details/79583551 #删除列 df_2 = ...
- python-数据描述与分析2(利用Pandas处理数据 缺失值的处理 数据库的使用)
2.利用Pandas处理数据2.1 汇总计算当我们知道如何加载数据后,接下来就是如何处理数据,虽然之前的赋值计算也是一种计算,但是如果Pandas的作用就停留在此,那我们也许只是看到了它的冰山一角,它 ...
随机推荐
- Ubuntu更换国内源
打开终端,输入:sudo gedit /etc/apt/sources.list 在文件最底部输入以下内容: deb http://mirrors.ustc.edu.cn/ubuntu/ xenial ...
- 只查看xilong.txt[共100行]内第20行到第30行的内容
1: Test Environment [root@xilong startimes]# seq > xilong.txt [root@xilong startimes]# cat xilong ...
- yii 后台配置独立子域名方法
我这里安装的是宝塔面板集成的环境WNMP,官网上虽然也有,但是写的并不明确,对我这种用YII的新手来说也很头疼,折腾了半天终于弄好,记录一下. 首先解析一个子域名:back.domain.com: 用 ...
- 关于具有I2C总线的TEA6320的使用
现在先了解一下TEA6320,TEA6320是一个I2C总线控制音响应用的立体声放大器,,它的I2C协议和音量控制如下: 它的主要代码: void delay1ms(unsigned int Dela ...
- UART学习之路(四)VerilogHDL实现的简单UART,VIVADO下完成仿真
用VerilogHDL实现UART并完成仿真就算是对UART整个技术有了全面的理解,同时也算是Verilog入门了.整个UART分为3部分完成,发送模块(Transmitter),接收模块(Recei ...
- Go语言的接口与反射
美女图片没啥用,就是为了好看 本文还在完善中... go总体而言是一门比较好入门的语言,许多特性都很精简易懂,但是接口与反射除外.他们真的让人头疼,不知道是自身资质问题还是怎么着,总是觉得很多书上写的 ...
- Shell--cut用法
cut是以每一行为一个处理对象的,这种机制和sed一样. cut接受三个定位方法: 1)byte: -b 2)characters: -c 3)fields: -d eg:提取第3,4,5,9的字节: ...
- 笔记-django-视图
笔记-django-视图 1. dispatch 1.1. overview To design URLs for an app, you create a Python module ...
- 成都Uber优步司机奖励政策(2月22日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 西安Uber优步司机奖励政策(8月10日到8月16日)
1) 工作日(周一到周五)早高峰时间段(7点到9:30点).晚高峰时间段(5点到8点)车费 2.0 倍,每单奖励部分上限35元 例:在高峰时段中,假设行程基本车费为¥15,只要达到奖励前提,最后你将获 ...