我试图将这些信息发送到数据帧,我得到一个空的数据帧。这是我从API获得的信息示例:
[[{'$type': 'Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities', 'id': '1', 'name': '1', 'modeName': 'bus', 'disruptions': [], 'created': '2019-08-20T16:25:25.377Z', 'modified': '2019-08-20T16:25:25.377Z', 'lineStatuses': [], 'routeSections': [{'$type': 'Tfl.Api.Presentation.Entities.MatchedRoute, Tfl.Api.Presentation.Entities', 'name': 'New Oxford Street - Canada Water Bus Station', 'direction': 'outbound', 'originationName': 'New Oxford Street', 'destinationName': 'Canada Water Bus Station', 'originator': '490000235Z', 'destination': '490004733D', 'serviceType': 'Regular', 'validTo': '2019-12-23T00:00:00Z', 'validFrom': '2019-08-17T00:00:00Z'}, {'$type': 'Tfl.Api.Presentation.Entities.MatchedRoute, Tfl.Api.Presentation.Entities', 'name': 'Canada Water Bus Station - Tottenham Court Road', 'direction': 'inbound', 'originationName': 'Canada Water Bus Station', 'destinationName': 'Tottenham Court Road', 'originator': '490004733C', 'destination': '490000235N', 'serviceType': 'Regular', 'validTo': '2019-12-23T00:00:00Z', 'validFrom': '2019-08-17T00:00:00Z'}], 'serviceTypes': [{'$type': 'Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities', 'name': 'Regular', 'uri': '/Line/Route?ids=1&serviceTypes=Regular'}], 'crowding': {'$type': 'Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities'}}, {'$type': 'Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities', 'id': '100', 'name': '100', 'modeName': 'bus', 'disruptions': [], 'created': '2019-08-20T16:25:25.367Z', 'modified': '2019-08-20T16:25:25.367Z', 'lineStatuses': [], 'routeSections': [{'$type': 'Tfl.Api.Presentation.Entities.MatchedRoute, Tfl.Api.Presentation.Entities', 'name': "King Edward Street / St Pauls Station - St George's Town Hall / Shadwell Stn", 'direction': 'outbound', 'originationName': 'King Edward Street / St Pauls Station', 'destinationName': "St George's Town Hall / Shadwell Stn", 'originator': '490008743N', 'destination': '490012020A', 'serviceType': 'Regular', 'validTo': '2019-12-23T00:00:00Z', 'validFrom': '2019-08-17T00:00:00Z'} 我需要在数据帧中获取此信息,所以我尝试了这段代码:
info2 = np.squeeze(info2).tolist() dftypes = pd.DataFrame(columns = ["id", "name", "modeName", "routeSections"]) dfroutes=pd.DataFrame(columns =["$type","name","direction","originationName","destinationName","serviceType"]) i=0 j=0 for dic in info2: for key in dic: if key in dftypes.columns.tolist(): dftypes.loc[i,key]=str(dic[key])
if key=='routeSections':
for dic2 in dic[key]:
for key2 in dic2:
if key2 in dfroutes.columns.tolist():
dfroutes.loc[j,key2]=str(dic2[key2])
j+=1
i+=1
dftypes 我有一个空的数据帧。我想从routeSections,name,modeName等获取所有信息。
你能告诉我正确的方法吗?
你可能最好pd.read_json直接使用TfL API。例如:
import pandas as pd x = pd.read_json('https://api.tfl.gov.uk/StopPoint/490006192S/arrivals')
$type 6 non-null object
id 6 non-null int64
operationType 6 non-null int64
vehicleId 6 non-null object
naptanId 6 non-null object
stationName 6 non-null object
lineId 6 non-null object
lineName 6 non-null object
platformName 6 non-null object
direction 6 non-null object
bearing 6 non-null int64
destinationNaptanId 6 non-null object
destinationName 6 non-null object
timestamp 6 non-null datetime64[ns, UTC]
timeToStation 6 non-null int64
currentLocation 6 non-null object
towards 6 non-null object
expectedArrival 6 non-null object
timeToLive 6 non-null object
modeName 6 non-null object
timing 6 non-null object
dtypes: datetime64ns, UTC, int64(4), object(16)
然后,您可以直接在Pandas中操作数据。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。