字典列表到熊猫数据框

时间:2019-09-16 13:57:03

标签: python pandas

我很难确定以下内容。我有以下格式的字典。

x = dict(zip(df2['instrumentId'],df2['relatedInstruments']))
print(x.items())

dict_items([('1', "[{u'instrumentId': u'123456x', u'startDate': u'2000-03-06', u'relationshipType': u'144A'}]"), ('2', "[{u'instrumentId': 123456xx', u'startDate': u'2000-11-17', u'relationshipType': u'144A'}, {u'instrumentId': u'A9306xx', u'startDate': u'2000-11-17', u'relationshipType': u'REGS'}]"), ('3', "[{u'instrumentId': u'A13635xx', u'startDate': u'1998-02-23', u'relationshipType': u'144A'}]"),....

本质上,Im试图做的是将每个相关的乐器分配给一个instrumentId,它可以是多个值。结果应如下所示。

enter image description here

我尝试使用DataFrame(),DataFrame.from_records()和.from_dict(),但是没有运气。解决这个问题的正确方法是什么?

谢谢!

编辑:

这就是原始数据框的样子

df2[['instrumentId','relatedInstruments']].head(2)

instrumentId    relatedInstruments
A123456 [{u'instrumentId': u'A1657501', u'startDate': u'2011-06-14', u'relationshipType': u'144A', u'endDate': u'2018-06-16'}]
A321456 [{u'instrumentId': u'A782951', u'startDate': u'2018-11-14', u'relationshipType': u'144A'}, {u'instrumentId': u'A782951', u'startDate': u'2011-04-05', u'relationshipType': u'144A', u'endDate': u'2018-11-14'}]

0 个答案:

没有答案