【数据处理】dataframe转换成JSON;JSON转换成dataframe;python、pandas库如何进行转换

一、json 格式转为 dataframe

from pandas.io.json import json_normalize
import pandas as pd
import json

1. 方法一:

data_str = open('movies.json').read()
df = pd.read_json(data_str,orient = 'records')

2. 方法二:

data_str = open('movies.json').read()
data_list = json.loads(data_str)
df_1 = json_normalize(data_list)

3. 方法三:

data_str = open('movies.json').read()
data_list = json.loads(data_str)
# d['title']中的'title'是json格式文件里面的名称
data = [[d['title'],d['score'],d['quote'],d['comment_num']] for d in data_list]
df_2 = pd.DataFrame(data,columns = ['title','score','quote','comment_num'])

二、DataFrame 转 json

import pandas as pd
from pandas import DataFrame as df

data = df([['a', 'b'], ['c', 'd']], index=['row 1', 'row 2'], columns=['col 1', 'col 2'])

1. 方法一:

json_columns = data.to_json(orient = "columns")  

返回结果: ‘{“col 1”:{“row 1”:“a”,“row 2”:“c”},“col 2”:{“row 1”:“b”,“row 2”:“d”}}’

json_split = data.to_json(orient = "split")   

返回结果: ‘{“columns”:[“col 1”,“col 2”],“index”:[“row 1”,“row 2”],“data”:[[“a”,“b”],[“c”,“d”]]}’

json_records = data.to_json(orient = "records")  

返回结果: ‘[{“col 1”:“a”,“col 2”:“b”},{“col 1”:“c”,“col 2”:“d”}]’

json_index = data.to_json(orient = "index")  

返回结果:‘{“row 1”:{“col 1”:“a”,“col 2”:“b”},“row 2”:{“col 1”:“c”,“col 2”:“d”}}’

json_values = data.to_json(orient = "values")  

返回结果: ‘[[“a”,“b”],[“c”,“d”]]’

2. 方法二:

json_dict = data.to_dict(orient = "dict")  

返回结果: {‘col 1’: {‘row 1’: ‘a’, ‘row 2’: ‘c’}, ‘col 2’: {‘row 1’: ‘b’, ‘row 2’: ‘d’}}

json_list = data.to_dict(orient = "list")  

返回结果: {‘col 1’: [‘a’, ‘c’], ‘col 2’: [‘b’, ‘d’]}

json_series = data.to_dict(orient = "series")  

返回结果: {‘col 1’: row 1 a row 2 c Name: col 1, dtype: object, ‘col 2’: row 1 b row 2 d Name: col 2, dtype: object}

json_split = data.to_dict(orient = "split")  

返回结果: {‘index’: [‘row 1’, ‘row 2’], ‘columns’: [‘col 1’, ‘col 2’], ‘data’: [[‘a’, ‘b’], [‘c’, ‘d’]]}

json_records = data.to_dict(orient = "records")  

返回结果: [{‘col 1’: ‘a’, ‘col 2’: ‘b’}, {‘col 1’: ‘c’, ‘col 2’: ‘d’}]

json_index = data.to_dict(orient = "index")  

返回结果:{‘row 1’: {‘col 1’: ‘a’, ‘col 2’: ‘b’}, ‘row 2’: {‘col 1’: ‘c’, ‘col 2’: ‘d’}}

三、输出Json文件

如果要输出JSON文件

将上述得出的字典进行编码 json.dumps() 转化成 json 形式即可:

json.dumps(json_dict)
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别出BUG求求了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值