开发者社区> 问答> 正文

如何将信息提供给数据框?

我试图将这些信息发送到数据帧,我得到一个空的数据帧。这是我从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等获取所有信息。

你能告诉我正确的方法吗?

展开
收起
游客6qcs5bpxssri2 2019-09-08 21:17:49 760 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    你可能最好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中操作数据。

    2019-09-08 21:18:36
    赞同 展开评论 打赏
问答分类:
API
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载