将 JSON 扁平化为 Pandas 数据框
Flatterning JSON to Pandas Dataframe
我正在尝试将此 json 扁平化为 pandas 数据框,但它对我来说越来越好。
[{
'contact': {
'id': 101,
'email': 'email1@address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
},
{
'contact': {
'id': 102,
'email': 'email2@address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
}
]
我正在寻找以下列:Id、Email、MarketingId、IsOptedIn、DateModifed。
尽管 marketingPreference 是一个数组,但里面只有一个 json 对象。
您可以使用pd.json_normalize
df = pd.json_normalize(data, record_path='marketingPreference', meta=[['contact', 'id'], ['contact', 'email']])
print(df)
marketingId isOptedIn dateModifed contact.id contact.email
0 1093 True 2022-05-10T14:29:24Z 101 email1@address.com
1 1093 True 2022-05-10T14:29:24Z 102 email2@address.com
我正在尝试将此 json 扁平化为 pandas 数据框,但它对我来说越来越好。
[{
'contact': {
'id': 101,
'email': 'email1@address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
},
{
'contact': {
'id': 102,
'email': 'email2@address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
}
]
我正在寻找以下列:Id、Email、MarketingId、IsOptedIn、DateModifed。
尽管 marketingPreference 是一个数组,但里面只有一个 json 对象。
您可以使用pd.json_normalize
df = pd.json_normalize(data, record_path='marketingPreference', meta=[['contact', 'id'], ['contact', 'email']])
print(df)
marketingId isOptedIn dateModifed contact.id contact.email
0 1093 True 2022-05-10T14:29:24Z 101 email1@address.com
1 1093 True 2022-05-10T14:29:24Z 102 email2@address.com