正如标题所示,我陷入了一种需要将数据帧的一行分解为多行的情况。数据框的当前状态:import pandas as pdop_d = {'A': [['NULL', 'NULL','NULL','NULL','NULL']], 'V':[[{'2016': '488.386000'},{'2017': '469.508000'}, {'2018': '445.510000'},{'2019': '579.299000'}, {'2020': '493.786000'}]], 'Q':[[{'2016': '488.386000'},{'2017': '469.508000'}, {'2018': '445.510000'},{'2019': '579.299000'}, {'2020': '493.786000'}]], 'Z': [['NULL', 'NULL','NULL','NULL','NULL']]}df = pd.DataFrame(op_d)数据框的期望状态:op_d2 = {'Date':[2016,2017,2018,2019,2020],'A': ['NULL', 'NULL','NULL','NULL','NULL'], 'V':[488.386000,469.508000,445.510000,579.299000,493.786000], 'Q':[488.386000,469.508000,445.510000,579.299000,493.786000], 'Z': ['NULL', 'NULL','NULL','NULL','NULL']}result_df = pd.DataFrame(op_d2)result_df我所做的是将字典的键放入单独的“日期”列中,并保持列的值不变。无论它是 NULL,它都将保持原样。所有列都会有类似的字典。即相同的日期范围。我收到 json 格式的原始响应,将其转换为数据帧以执行一些计算。我将不胜感激任何帮助。原始 json 响应: { 'Prop1': '', 'Prop2': ['Somedata', 'SomeData2'], 'Prop3': 5, 'Prop4': '', 'StartDate': '', 'Function': 'ABC', 'Identifier': 'ID001', 'Col2': 2, 'Prop5': 'XYZ', 'Frequency': '', 'Limit': '', 'Rows': [{'Row': ['136.731000', '2016']}, {'Row': ['135.331000', '2017']}, {'Row': ['163.965000', '2018']}, {'Row': ['153.371000', '2019']}, {'Row': ['161.864000', '2020']}]},}
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
虽然我无法对效率做出任何声明,但也许这可以满足您的需求。
op_d_new = {k:(v[0] if v[0][0]=='NULL' else \
[list(j.values())[0] for j in v[0]]) for k,v in op_d.items()}
for v in op_d.values():
if v[0][0] == 'NULL':
continue
dates = [list(j.keys())[0] for j in v[0]]
break
op_d_new.update({'Dates':dates})
df = pd.DataFrame(op_d_new)
df
A V Q Z Dates
0 NULL 488.386000 488.386000 NULL 2016
1 NULL 469.508000 469.508000 NULL 2017
2 NULL 445.510000 445.510000 NULL 2018
3 NULL 579.299000 579.299000 NULL 2019
4 NULL 493.786000 493.786000 NULL 2020
添加回答
举报
0/150
提交
取消