Python与JSON转化+Dataframe按不同形式与JSON转化

你要的Json格式转化都在这里~~

1、json.dumps: 用于将 Python 对象编码成 JSON 字符串

import json
data = [{ 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 }]
#data = { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 }
json = json.dumps(data)
print(type(json),json)  #不管是列表合适字典;转化为JSON合适就是在外面加一个“”,变为字符串

>>> [{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}]
##>>> {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}

2、json.loads:用于解码 JSON 数据。该函数返回 Python 字段的数据类型

import json
jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
text = json.loads(jsonData)
print(text)

>>>{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

3、json.dump() 和 json.load() 来编码和解码JSON数据,用于处理文件

with open('test.json', 'w') as f:  #以写的方式打开文件
    json.dump(data, f)   #python对象编码为json字符串后写入.json文件
 
with open('test.json', 'r') as f:  #以读的方式打开文件
    data = json.load(f)  #读取json字符串并解码成python对象编码,保存到data变量里

4、pandas.Dataframe转化为Json:to_json
(1)如果是Series转json,默认的orient是’index’,orient可选参数有 {‘split’,‘records’,‘index’}
(2)如果是DataFrame转json,默认的orient是’columns’,orient可选参数有 {‘split’,‘records’,‘index’,‘columns’,‘values’}

  • 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类似
import pandas as pd
df = pd.DataFrame([['a', 'b'], ['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])
df

在这里插入图片描述

df.to_json(orient='records')
>>>'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'

df.to_json(orient='index')
>>>'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'

df.to_json(orient='split')
>>>'{"columns":["col 1","col 2"],"index":["row 1","row 2"],"data":[["a","b"],["c","d"]]}'

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"}]}'

5、eval() 函数:用来执行一个字符串表达式,并返回表达式的值。可以提取字符串中的内容:

a = {'test_data':'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'}
b = a['test_data']
c = eval(b)
data = pd.DataFrame(c)
data

在这里插入图片描述
6、pandas.DataFrame.to_dict
(1)DataFrame.to_dict(self, orient=‘dict’, into=)
(2)关键参数:orient : str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}

df = pd.DataFrame({'col1': [1, 2],'col2': [0.5, 0.75]},index=['row1', 'row2'])
df

在这里插入图片描述

df.to_dict()
>>>{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}

df.to_dict('series')
>>>{'col1': row1    1
 row2    2
 Name: col1, dtype: int64, 'col2': row1    0.50
 row2    0.75
 Name: col2, dtype: float64}
 
df.to_dict('split')
>>>{'index': ['row1', 'row2'],
 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]]}

df.to_dict('records')
>>>[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]

df.to_dict('index')
>>>{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}

你可能感兴趣的:(Python)