futui 发表于 2023-4-11 08:36:15

关于用字典构建数据帧的问题,请大师帮忙

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) #提出数据
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

请问错在哪?请大师帮忙修改下,谢谢

isdkz 发表于 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) #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame()

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) #提出数据
data_json = json.loads(data_list)['data']    #评价列表
df_data = pd.DataFrame(, columns=data_json.keys())

print(df_data)

在这个例子中,我们使用字典键作为数据帧的列名。
页: [1]
查看完整版本: 关于用字典构建数据帧的问题,请大师帮忙