Вот решение, использующее json_normalize
( документация ), при условии, что размещенный вами фрагмент JSON находится в словаре Python с именем d
.
from pandas.io.json import json_normalize
# Build a list of paths to JSON fields that will end up as metadata
# in the final DataFrame
meta = list(js['locations'][0].keys())
# meta is now this:
# ['timestampMs',
# 'latitudeE7',
# 'longitudeE7',
# 'accuracy',
# 'altitude',
# 'verticalAccuracy',
# 'activity']
# Almost correct. We need to remove 'activity' and append
# the list ['activity', 'timestampMs'] to meta.
meta.remove('activity')
meta.append(['activity', 'timestampMs'])
# meta is now this:
# ['timestampMs',
# 'latitudeE7',
# 'longitudeE7',
# 'accuracy',
# 'altitude',
# 'verticalAccuracy',
# ['activity', 'timestampMs']]
# Use json_normalize on the list of dicts
# that lives at d['locations'], passing in
# the appropriate record path and metadata
# paths, and specifying the double 'activity_'
# record prefix.
json_normalize(d['locations'],
record_path=['activity', 'activity'],
meta=meta,
record_prefix='activity_activity_')
activity_activity_confidence activity_activity_type timestampMs latitudeE7 longitudeE7 accuracy altitude verticalAccuracy activity.timestampMs
0 100 STILL 1549913792265 323518421 -546166813 13 1 2 1549913286057
1 100 DRIVING 1549913792265 323518421 -546166813 13 1 2 1549913730454
2 100 ACTIVE 1549912693813 323518421 -546166813 13 1 2 1549911547308
3 100 BIKING 1549912693813 323518421 -546166813 13 1 2 1549912330473
Редактировать
Если путь записи ['activity', 'activity']
иногда отсутствует, приведенный выше код выдаст ошибку.Следующий обходной путь должен работать для этого конкретного случая, но является хрупким и может быть недопустимо медленным в зависимости от размера ваших входных данных:
# Create an example by deleting one of the 'activity' paths
# from the original dict
del d['locations'][0]['activity']
pd.concat([json_normalize(x,
record_path=['activity', 'activity']
if 'activity' in x.keys() else None,
meta=meta,
record_prefix='activity_activity_')
for x in d['locations']],
axis=0,
ignore_index=True,
sort=False)
accuracy altitude latitudeE7 longitudeE7 timestampMs verticalAccuracy activity_activity_confidence activity_activity_type activity.timestampMs
0 13 1 323518421 -546166813 1549913792265 2 NaN NaN NaN
1 13 1 323518421 -546166813 1549912693813 2 100.0 ACTIVE 1549911547308
2 13 1 323518421 -546166813 1549912693813 2 100.0 BIKING 1549912330473