pandas.DataFrame.to_json按行转json的方法
作者:huanbia 时间:2022-11-09 11:24:23
最近需要将csv文件转成DataFrame并以json的形式展示到前台,故需要用到Dataframe的to_json方法
to_json方法默认以列名为键,列内容为值,形成{col1:[v11,v21,v31…],col2:[v12,v22,v32],…}这种格式,但有时我们需要按行来转为json,形如这种格式[row1:{col1:v11,col2:v12,col3:v13…},row2:{col1:v21,col2:v22,col3:v23…}]
通过查找官网我们可以看到to_json方法有一个参数为orient,其参数说明如下:
orient : string
Series
default is ‘index'
allowed values are: {‘split','records','index'}
DataFrame
default is ‘columns'
allowed values are: {‘split','records','index','columns','values'}
The format of the JSON string
split : dict like {index -> [index], columns -> [columns], data -> [values]}
records : list like [{column -> value}, … , {column -> value}]
index : dict like {index -> {column -> value}}
columns : dict like {column -> {index -> value}}
values : just the values array
table : dict like {‘schema': {schema}, ‘data': {data}} describing the data, and the data component is like orient='records'.
Changed in version 0.20.0
大致意思为:
如果是Series转json,默认的orient是'index',orient可选参数有 {‘split','records','index'}
如果是DataFrame转json,默认的orient是'columns',orient可选参数有 {‘split','records','index','columns','values'}
json的格式如下
split,样式为 {index -> [index], columns -> [columns], data -> [values]}
records,样式为[{column -> value}, … , {column -> value}]
index ,样式为 {index -> {column -> value}}
columns,样式为 {index -> {column -> value}}
values,数组样式
table,样式为{‘schema': {schema}, ‘data': {data}},和records类似
看一下官网给的demo
df = pd.DataFrame([['a', 'b'], ['c', 'd']],
index=['row 1', 'row 2'],
columns=['col 1', 'col 2'])
###########
split
###########
df.to_json(orient='split')
>'{"columns":["col 1","col 2"],
"index":["row 1","row 2"],
"data":[["a","b"],["c","d"]]}'
###########
index
###########
df.to_json(orient='index')
>'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
###########
records
###########
df.to_json(orient='index')
>'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
###########
table
###########
df.to_json(orient='table')
>'{"schema": {"fields": [{"name": "index", "type": "string"},
{"name": "col 1", "type": "string"},
{"name": "col 2", "type": "string"}],
"primaryKey": "index",
"pandas_version": "0.20.0"},
"data": [{"index": "row 1", "col 1": "a", "col 2": "b"},
{"index": "row 2", "col 1": "c", "col 2": "d"}]}'
主要参考官网API:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
来源:https://blog.csdn.net/huanbia/article/details/72674832
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
看ASP程序源码的方法及工具
Python3读取文件常用方法实例分析
python用Pygal如何生成漂亮的SVG图像详解
![](https://img.aspxhome.com/file/2023/3/131593_0s.gif)
Python中seaborn库之countplot的数据可视化使用
![](https://img.aspxhome.com/file/2023/6/62026_0s.png)
Vue watch监听使用的几种方法
python+matplotlib绘制旋转椭圆实例代码
![](https://img.aspxhome.com/file/2023/2/81782_0s.png)
在Python的Django框架中创建和使用模版
Python实现通过文件路径获取文件hash值的方法
原生js编写贪吃蛇小游戏
Linux下远程连接Jupyter+pyspark部署教程
vue中的mescroll搜索运用及各种填坑处理
深度定制Python的Flask框架开发环境的一些技巧总结
JS性能优化笔记搜索整理
如何编写CSS代码才能更有效率
利用一个简单的例子窥探CPython内核的运行机制
一文学会利用python解决文章付费限制问题
![](https://img.aspxhome.com/file/2023/7/82537_0s.jpg)
教你使用python做一个“罚点球”小游戏
js+css实现有立体感的按钮式文字竖排菜单效果
![](https://img.aspxhome.com/file/2023/8/135738_0s.jpg)
Anaconda超详细保姆级安装配置教程
![](https://img.aspxhome.com/file/2023/7/90917_0s.png)
Python学习之包与模块详解
![](https://img.aspxhome.com/file/2023/7/66867_0s.jpg)