gpt4 book ai didi

python - 字节/字典到数据帧

转载 作者:行者123 更新时间:2023-12-01 01:05:37 28 4
gpt4 key购买 nike

将数据转换为 DataFrame 的最佳方法是什么?

    data = b'{"word": "Gondwana", "date": "2019-03-27 13:07:12.404732"}'
b'{"word": "alalus", "date": "2019-03-27 13:07:12.909517"}'
b'{"word": "Balto-Slavonic", "date": "2019-03-27 13:07:14.911308"}'
b'{"word": "peculatation", "date": "2019-03-27 13:07:15.421915"}'

我试过这个。似乎不起作用。

d = pd.DataFrame(dict(data))

最佳答案

首先将值解码为 utf-8 并通过 ast.literal_evaljson.loads< 转换为列表理解中的字典:

data = [b'{"word": "Gondwana", "date": "2019-03-27 13:07:12.404732"}',
b'{"word": "alalus", "date": "2019-03-27 13:07:12.909517"}',
b'{"word": "Balto-Slavonic", "date": "2019-03-27 13:07:14.911308"}',
b'{"word": "peculatation", "date": "2019-03-27 13:07:15.421915"}']

import ast

df = pd.DataFrame([ast.literal_eval(x.decode("utf-8")) for x in data])
print (df)
date word
0 2019-03-27 13:07:12.404732 Gondwana
1 2019-03-27 13:07:12.909517 alalus
2 2019-03-27 13:07:14.911308 Balto-Slavonic
3 2019-03-27 13:07:15.421915 peculatation

替代解决方案,在大数据中应该更快:

import json

df = pd.DataFrame([json.loads(x.decode("utf-8")) for x in data])

关于python - 字节/字典到数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55397539/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com