用途:
df = pd.DataFrame( {'col': {0: {'attributes': {'type': 'Contact', 'url': '/services/data/v38.0/sobjects/Contact/0035B00000KRMhZQAX'},
'Id': '0035B00000KRMhZQAX', 'Name': 'abc xyz', 'CustId': 'hello00'}}} )
df.loc[1, 'col'] = 10
df.loc[2, 'col'] = np.nan
print (df)
col
0 {'attributes': {'type': 'Contact', 'url': '/se...
1 10
2 NaN
首先使用字典仅筛选值:
df = df[df['col'].apply(lambda x: isinstance(x, dict))]
print (df)
col
0 {'attributes': {'type': 'Contact', 'url': '/se...
按键排除,如果键不存在,则添加
None
:
df1 = pd.DataFrame([(x.get('Id', None), x.get('Name', None), x.get('CustId', None))
for x in df['col']], columns=['Id','Name','CustId'])
print (df1)
Id Name CustId
0 0035B00000KRMhZQAX abc xyz hello00