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设计】。

相关文章

Python获取文件所在目录和文件名的方法

Python获取文件所在目录和文件名的方法

实例如下: import os if __name__ == "__main__": file_path = 'D:/test/test.apk' parent_p...

Python中的两个内置模块介绍

使用了Python一段时间后,可以说Python的基本单位就是模块了,在使用模块的时候我们一般会使用通过import语句来将其导入,但是我们在没有导入任何模块的时候,我们却能使用这样的一...

Python实现删除列表中满足一定条件的元素示例

本文实例讲述了Python实现删除列表中满足一定条件的元素。分享给大家供大家参考,具体如下: 从列表中删除满足一定条件的元素。 如:删除一个列表中长度为0的元素,或者删除列表中同时是2和...

详解python 模拟豆瓣登录(豆瓣6.0)

详解python 模拟豆瓣登录(豆瓣6.0)

最近在学习python爬虫,看到网上有很多关于模拟豆瓣登录的例子,随意找了一个试了下,发现不能运行,对比了一下代码和豆瓣网站,发现原来是豆瓣网站做了修改,增加了反爬措施。 首先看下要模拟...

通过C++学习Python

我会随便说,C++ 近年来开始"抄袭" Python 么?我只会说,我在用 C++ 来学习 Python. 不信?来跟着我学? 字面量 Python 早在 2.6 版本中就支持将二进制作...