多字典形成数据帧的问题,求帮忙,谢谢
我的代码如下 ,但运行不了,请求大师帮忙解决或是修改我的代码rr = '''势*/var Data_grandTotal = [{"name":"华夏成长混合","data":[,,]},{"name":"同类平均","data":[,,]},{"name":"沪深100","data":[,,]}];/*同'''
result = re.findall(r'var Data_grandTotal = \[(\{.*?\})\];', rr)
data_ljsy = json.loads(result )
data_dic = {} # 获取数据部分并转换为字典形式
for item in data_ljsy:
data_dic] = item['data']
data_dic[['时间']] = item['data']
df = pd.DataFrame(data_dic)
print(df)
我想要的结果:
时间 华夏成长混合 同类平均 沪深100
1664467200000 0 0 0
1665331200000 -1.54 -1.66 -2.21
1680192000000 -10.12 2.3 6.47 import re
import json
import pandas as pd
rr = '''势*/var Data_grandTotal = [{"name":"华夏成长混合","data":[,,]},{"name":"同类平均","data":[,,]},{"name":"沪深100","data":[,,]}];/*同'''
result = re.findall(r'var Data_grandTotal = (\[.*?\]);', rr)
data_ljsy = json.loads(result)
data_dic = {}
for item in data_ljsy:
for data in item['data']:
if data not in data_dic:
data_dic] = {}
data_dic]] = data
df = pd.DataFrame(data_dic).T.reset_index()
df.columns.name = None
df.rename(columns={"index": "时间"}, inplace=True)
print(df)
页:
[1]