DataFrame转化为json的方法教程

网络上有好多的教程,讲得不太清楚和明白,我用实际的例子说明了一下内容,附档代码,方便理解和使用 

DataFrame.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=True, indent=None) [source]

将对象转换为JSON字符串。

注意:NaNNone将被转换为nulldatetime对象将被转换为UNIX时间戳。

参数:

path_or_buf :str 或 file handle, 默认为 None

文件路径或对象。如果未指定,则结果以字符串形式返回。

orient :str

预期的JSON字符串格式的指示。

1) Series:

默认值为‘index’

允许的值为:{'split','records','index','table'}

2) DataFrame:

默认为‘columns’

允许的值为:

{'split','records','index','columns','values','table'}

3) JSON字符串格式:

'split':类似{'index'-> [index],

'columns'-> [columns],'data'-> [values]}的字典

'records':类似于[{column-> value},…,{column-> value}]的列表

'index':类似{index->​​ {column-> value}}的字典

'columns':类似{column-> {index->​​ value}}的字典

'values’:只是值数组

'table':类似{'schema':{schema},'data':{data}}的字典

描述数据,其中数据成分类似于orient='records'

在版本0.20.0中更改。

date_format :{None, ‘epoch’, ‘iso’}

日期转换的类型。'epoch'= epoch milliseconds

'iso'= ISO8601。默认值取决于orient

对于 orient='table',默认值为'iso'

对于所有其他东方,默认值为‘epoch’.。

double_precision :int, 默认为10

在对浮点值进行编码时要使用的小数位数。

force_ascii :bool, 默认为True

强制将字符串编码为ASCII

date_unit :str,默认为“ms”(毫秒)

要编码的时间单位,控制时间戳和ISO8601精度。

“s”,“ms”,“us”,“ns”之一分别表示秒,毫秒,微秒和纳秒。

default_handler :callable, 默认为None

如果对象不能转换为适合JSON的格式,则调用。

应该接收一个参数,该参数是要转换的对象并返回一个可序列化对象。

lines :bool, 默认为 False

如果'orient''records',则写出行分隔的json格式。

如果不正确的‘orient’将抛出ValueError,因为其他人没有列出。

compression :{‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}

表示要在输出文件中使用的压缩的字符串,

仅在第一个参数是文件名时使用。默认情况下,

压缩是从文件名推断出来的。

在0.24.0版本中更改:增加了“infer”选项并将其设置为默认

index :bool, 默认为 True

是否在JSON字符串中包括索引值。

仅当Orient“split”“table”时,

才支持不包括index(index=False)。

0.23.0版中的新功能。

indent int, 可选

用于缩进每条记录的空白长度。

1.0.0版的新功能。。

返回值:

None 或 str

如果path_or_bufNone

则将生成的json格式作为字符串返回。

否则返回None

Notes

indent=0的行为与stdlib不同,stdlib不会缩进输出,但会插入新行。目前,在panda中,indent=0和默认的indent=None是等价的,不过在将来的版本中可能会更改。

df = pd.DataFrame( [["A0001", "张三"], ["A0002", "李四"]], index=["row 1", "row 2"],columns=["工号", "姓名"] )
print('-------------------------------------------')
print(df)
print('index')
print(df.to_json(orient='index',force_ascii=False))
print('columns')
print(df.to_json(orient='columns',force_ascii=False))
print('split')
print(df.to_json(orient='split',force_ascii=False))
print('records')
print(df.to_json(orient='records',force_ascii=False))
mydate={"parts":df.to_json(orient='records',force_ascii=False)}
print(mydate)
print('table')
print(df.to_json(orient='table',force_ascii=False))
print('values')
print(df.to_json(orient='values',force_ascii=False))
print('-------------------------------------------')
#遍历
for index, row in df.iterrows():
    print(index)
    print(row)

输出内容,理解转化在json的内容的逻辑

-------------------------------------------
          工号  姓名
row 1  A0001  张三
row 2  A0002  李四
index
{"row 1":{"工号":"A0001","姓名":"张三"},"row 2":{"工号":"A0002","姓名":"李四"}}
columns
{"工号":{"row 1":"A0001","row 2":"A0002"},"姓名":{"row 1":"张三","row 2":"李四"}}
split
{"columns":["工号","姓名"],"index":["row 1","row 2"],"data":[["A0001","张三"],["A0002","李四"]]}
records
[{"工号":"A0001","姓名":"张三"},{"工号":"A0002","姓名":"李四"}]
{'parts': '[{"工号":"A0001","姓名":"张三"},{"工号":"A0002","姓名":"李四"}]'}
table
{"schema":{"fields":[{"name":"index","type":"string"},{"name":"工号","type":"string"},{"name":"姓名","type":"string"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":"row 1","工号":"A0001","姓名":"张三"},{"index":"row 2","工号":"A0002","姓名":"李四"}]}
values
[["A0001","张三"],["A0002","李四"]]
-------------------------------------------
row 1
工号    A0001
姓名       张三
Name: row 1, dtype: object
row 2
工号    A0002
姓名       李四
Name: row 2, dtype: object

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值