pandas.DataFrame.to_json按行转json的方法

yipeiwu_com6年前Python基础

最近需要将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

以上这篇pandas.DataFrame.to_json按行转json的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用pytorch进行图像的顺序读取方法

产生此次实验的原因:当我使用pytorch进行神经网络的训练时,需要每次向CNN传入一组图像,并且这些图片的存放位置是在两个文件夹中: A文件夹:图片1a,图片2a,图片3a……图片10...

Python中的闭包实例详解

一般来说闭包这个概念在很多语言中都有涉及,本文主要谈谈python中的闭包定义及相关用法。Python中使用闭包主要是在进行函数式开发时使用。详情分析如下: 一、定义 python中的闭...

opencv python 图像去噪的实现方法

opencv python 图像去噪的实现方法

在早先的章节里,我们看到很多图像平滑技术如高斯模糊,Median模糊等,它们在移除数量小的噪音时在某种程度上比较好用。在这些技术里,我们取像素周围的一小部分邻居,做一些类似于高斯平均权重...

解决Python 命令行执行脚本时,提示导入的包找不到的问题

解决Python 命令行执行脚本时,提示导入的包找不到的问题

在Pydev能正常执行的脚本,在导出后在命令行执行,通常会报自己写的包导入时找不到。 一:报错原因 在PyDev中,test.py 中导入TestUserCase里面的py文件时,会写...

python中如何使用分步式进程计算详解

python中如何使用分步式进程计算详解

前言 在python中使用多进程和多线程都能达到同时运行多个任务,和多进程和多线程的选择上,应该优先选择多进程的方式,因为多进程更加稳定,且对于进程的操作管理也更加方便,但有一点是多进程...