我将这个SFDC SOQL的输出保存到一个数据帧中得到了一个称为“线索”的数据帧。我一直在尝试扩展列“ Leads__r.record”
Company Month Amount Leads__r.done Leads__r.record Leads__r.totalSize
0 A1 September 500000 True [{u'Id': u'Q500, u'Company': u'... 1.0
1 B1 December 16200 True [{u'Id': u'Q600', u'Company': u'... 1.0
2 C1 December 35000 True [{u'Id': u'Q700', u'Company': u'... 1.0
3 D1 December 16200 True [{u'Id': u'Q800', u'Company': u'... 1.0
4 E1 December 40000 True [{u'Id': u'Q900', u'Company': u'... 1.0
5 F1 December 54000 True [{u'Id': u'Q950', u'Company': u'... 1.0
6 G1 August 67700 True [{u'Id': u'Q1000', u'Company': u'... 1.0
这是Leads ['Leads__r.record']的输出:
leads['Leads__r.record']
0 [{u'Id': u'Q500', u'Company': u'...
1 [{u'Id': u'Q600', u'Company': u'...
2 [{u'Id': u'Q700', u'Company': u'...
3 [{u'Id': u'Q800', u'Company': u'...
4 [{u'Id': u'Q900', u'Company': u'...
5 [{u'Id': u'Q950', u'Company': u'...
线索的输出['Leads__r.record'] [0]
[{u'Company': u'A1',
u'CurrencyIsoCode': u'AUD',
u'Date_Submitted__c': u'2018-09-12',
u'Deal_Amount__c': 500000,
u'Id': u'Q500',
u'DEAL_ID': u'a78989',
u'PrimaryProductFamily__c': u'Cloud',
u'Primary_Product_Family__c': u'Cloud',
u'Product_interest__c': None,
u'attributes': {u'type': u'Lead',
u'url': u'/services/data/v24.0/sobjects/Lead/00Qf200001Zls5CEAR'}}]
我已经尝试了所有方法:
pd.concat([pd.DataFrame(x) for x in leads['Leads__r.record']],
keys=leads['Leads__r.totalSize']).reset_index(level=1, drop=True).reset_index()
上面的方法可以正常工作,但是由于某种原因,我一直报错,ValueError:DataFrame构造函数未正确调用!
leads['Leads__r.records'].reset_index().drop('index',1)
这是我最近来的
pd.concat([pd.DataFrame(x) for x in leads['Leads__r.records'][0:200]], keys=leads.index).reset_index(level=1, drop=True).reset_index()
这有效,但仅适用于前200行
答案 0 :(得分:0)
按如下所示尝试json_normalize
:
from pandas.io.json import json_normalize
# Convert the column of single-element lists to a list of dicts
records = [x[0] for x in df['Leads__r.record']]
# normalize the records and concat with the original df
res = pd.concat([df.drop('Leads__r.record', axis=1), json_normalize(records)], axis=1)