鱼C论坛

 找回密码
 立即注册
查看: 2134|回复: 1

[已解决]关于用字典构建数据帧的问题,请大师帮忙

[复制链接]
发表于 2023-4-11 08:36:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import re
import json
import pandas as pd

data = '''jQuery35102226856188430557_1672618613581({"rc":0,"rt":4,"svr":182482656,"lt":1,"full":1,"dlmkts":"","data":{"f43":27169,"f44":27169,"f45":27169,"f46":27169,"f47":0,"f57":"CRB","f58":"路透CRB商品指数","f59":2,"f60":27199,"f86":1681153200,"f107":100,"f152":2,"f169":-30,"f170":-11,"f171":0,"f292":5}});'''
data_list = re.findall(r'(\{.*\})',data)[0] #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame(data_json)

print(df_data)
运行出现ValueError: If using all scalar values, you must pass an index

请问错在哪?请大师帮忙修改下,谢谢
最佳答案
2023-4-11 08:38:17
您的代码几乎没有问题,只是在构建数据帧时,您需要将字典数据转换为数据帧的行。

以下是一个修复后的代码示例:
import re
import json
import pandas as pd

data = '''jQuery35102226856188430557_1672618613581({"rc":0,"rt":4,"svr":182482656,"lt":1,"full":1,"dlmkts":"","data":{"f43":27169,"f44":27169,"f45":27169,"f46":27169,"f47":0,"f57":"CRB","f58":"路透CRB商品指数","f59":2,"f60":27199,"f86":1681153200,"f107":100,"f152":2,"f169":-30,"f170":-11,"f171":0,"f292":5}});'''
data_list = re.findall(r'(\{.*\})',data)[0] #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame([data_json])

print(df_data)

修改后,代码将data_json字典包装在一个列表中,以便将其转换为数据帧。您还可以在代码中添加列索引(列名):
import re
import json
import pandas as pd

data = '''jQuery35102226856188430557_1672618613581({"rc":0,"rt":4,"svr":182482656,"lt":1,"full":1,"dlmkts":"","data":{"f43":27169,"f44":27169,"f45":27169,"f46":27169,"f47":0,"f57":"CRB","f58":"路透CRB商品指数","f59":2,"f60":27199,"f86":1681153200,"f107":100,"f152":2,"f169":-30,"f170":-11,"f171":0,"f292":5}});'''
data_list = re.findall(r'(\{.*\})',data)[0] #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame([data_json], columns=data_json.keys())

print(df_data)

在这个例子中,我们使用字典键作为数据帧的列名。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-11 08:38:17 | 显示全部楼层    本楼为最佳答案   
您的代码几乎没有问题,只是在构建数据帧时,您需要将字典数据转换为数据帧的行。

以下是一个修复后的代码示例:
import re
import json
import pandas as pd

data = '''jQuery35102226856188430557_1672618613581({"rc":0,"rt":4,"svr":182482656,"lt":1,"full":1,"dlmkts":"","data":{"f43":27169,"f44":27169,"f45":27169,"f46":27169,"f47":0,"f57":"CRB","f58":"路透CRB商品指数","f59":2,"f60":27199,"f86":1681153200,"f107":100,"f152":2,"f169":-30,"f170":-11,"f171":0,"f292":5}});'''
data_list = re.findall(r'(\{.*\})',data)[0] #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame([data_json])

print(df_data)

修改后,代码将data_json字典包装在一个列表中,以便将其转换为数据帧。您还可以在代码中添加列索引(列名):
import re
import json
import pandas as pd

data = '''jQuery35102226856188430557_1672618613581({"rc":0,"rt":4,"svr":182482656,"lt":1,"full":1,"dlmkts":"","data":{"f43":27169,"f44":27169,"f45":27169,"f46":27169,"f47":0,"f57":"CRB","f58":"路透CRB商品指数","f59":2,"f60":27199,"f86":1681153200,"f107":100,"f152":2,"f169":-30,"f170":-11,"f171":0,"f292":5}});'''
data_list = re.findall(r'(\{.*\})',data)[0] #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame([data_json], columns=data_json.keys())

print(df_data)

在这个例子中,我们使用字典键作为数据帧的列名。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-29 10:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表