我需要从JSON字符串中提取以下信息:
flightId lat lon
657853226 39.588 -123.6683
...
这就是我开始解决任务的方式:
request=Request('...')
response_flights = urlopen(request)
fn = response_flights.read()
flights = json.loads(fn)
flights = pd.DataFrame(json_normalize(flights['flightPositions']))
但是我可以保存DataFrame
中的位置以及flightId
吗?
答案 0 :(得分:1)
我们说我的输入文件input.json
看起来像
{
"flightPositions": [
{
"flightId": 65782839,
"positions": [
{
"lon": -123.6683,
"lat": 39.588
},
{
"lon": -123.734,
"lat": 39.6446
}
]
}
]
}
然后,我可以从DataFrame
对象列表中构建一个pandas Series
,如下所示:
import json
import pandas
data = json.load(open('input.json', 'r'))
flightdata = []
for flight_position in data['flightPositions']:
flight_id = flight_position['flightId']
for position in flight_position['positions']:
position['flightId'] = flight_id
series = pandas.Series(position)
flightdata.append(series)
df = pandas.DataFrame(flightdata)
print(df)
这会给我:
flightId lat lon
0 65782839 39.5880 -123.6683
1 65782839 39.6446 -123.7340