Вы можете выполнить некоторую предварительную обработку данных, чтобы получить желаемый результат.Вот одно из возможных решений для решения вашей задачи:
import pandas as pd
import json
def extend_coordinates(coordinates, c_id, geo_type):
result = []
for elem in coordinates:
if len(elem) == 2:
# normal case
latitude, longitude = elem
else:
latitude, longitude = [None, None]
result.append([c_id, geo_type, latitude, longitude])
return result
data_json = [
{
'C_ID' : '1',
'Latlong' : {
'__type' : 'GeoPoint',
'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
}
},
{
'C_ID' : '2',
'Latlong' : {
'__type' : 'GeoPoint',
'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
}
},
{
'C_ID' : '3',
'Latlong' : {
'__type' : 'GeoPoint',
'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
}
}]
data = pd.read_json(json.dumps(data_json))
data['Common'] = data.apply(lambda x: extend_coordinates(coordinates=x['Latlong'].get('latitude', [None, None]),
c_id=x['C_ID'],
geo_type=x['Latlong'].get('__type', None)), axis=1)
data_ext = pd.DataFrame(np.concatenate(data['Common']),
columns=['C_ID', '__type', 'latitude', 'longitude'])
# if data_ext dataframe is not enough, you can combine data to your desired output
data_ext['Latlong'] = data_ext.apply(lambda x: {'__type': x['__type'],
'latitude': x['latitude'],
'longitude': x['longitude']}, axis=1)
result = data_ext[['C_ID', 'Latlong']]
del data_ext, data