代码之家  ›  专栏  ›  技术社区  ›  AlexW

pandas-从json数据创建数据帧,具体包括哪些列

  •  1
  • AlexW  · 技术社区  · 6 年前

    我正在用下面的命令创建我的数据帧

    getting data from url
    ...
    devices = get_device_data.json()
    device_data = devices["data"]
    p_dev = pd.DataFrame(device_data)
    

    谢谢

    [
        {
            "id": 474378238
            "account": "https: //www.****.com/api/v2/accounts/38021/",
            "bsid": None,
            "carrier": "BOB",
            "carrier_id": "BOB BOB",
            "channel": None,
            "connection_state": "connected",
            "gsn": "356853050758871",
            "homecarrid": "BOB",
            "hostname": "BOB1345345",
            "is_asset": True,
            "is_gps_supported": True,
            "is_upgrade_available": False,
            "is_upgrade_supported": True,
            "ltebandwidth": "20 MHz",
            "mac": None,
            "serial": "356853050758871",
            "service_type": "LTE",
            "ssid": None,
            "summary": "connected",
            "txchannel": "19667",
            "type": "mdm",
            "uid": "f5a8da8f",
            "updated_at": "2018-08-17T11:19:57.019938+00:00",
            "uptime": 86412.8558200002,
        },
        {
            "id": 5674657356
            "account": "https: //www.****.com/api/v2/accounts/38021/",
            "bsid": None,
            "carrier": "BOB",
            "carrier_id": "BOB BOB",
            "channel": None,
            "connection_state": "connected",
            "gsn": "356853050758871",
            "homecarrid": "BOB",
            "hostname": "BOB10765",
            "is_asset": True,
            "is_gps_supported": True,
            "is_upgrade_available": False,
            "is_upgrade_supported": True,
            "ltebandwidth": "20 MHz",
            "mac": None,
            "serial": "356853050758871",
            "service_type": "LTE",
            "ssid": None,
            "summary": "connected",
            "txchannel": "19667",
            "type": "mdm",
            "uid": "f5a8da8f",
            "updated_at": "2018-08-17T11:19:57.019938+00:00",
            "uptime": 86412.8558200002,
        },
        {
            "id": 5674657465
            "account": "https: //www.****.com/api/v2/accounts/38021/",
            "bsid": None,
            "carrier": "BOB",
            "carrier_id": "BOB BOB",
            "channel": None,
            "connection_state": "connected",
            "gsn": "356853050758871",
            "homecarrid": "BOB",
            "hostname": "BOB10453453",
            "is_asset": True,
            "is_gps_supported": True,
            "is_upgrade_available": False,
            "is_upgrade_supported": True,
            "ltebandwidth": "20 MHz",
            "mac": None,
            "serial": "356853050758871",
            "service_type": "LTE",
            "ssid": None,
            "summary": "connected",
            "txchannel": "19667",
            "type": "mdm",
            "uid": "f5a8da8f",
            "updated_at": "2018-08-17T11:19:57.019938+00:00",
            "uptime": 86412.8558200002,
        },
        {
            "id": 9756756756
            "account": "https: //www.****.com/api/v2/accounts/38021/",
            "bsid": None,
            "carrier": "BOB",
            "carrier_id": "BOB BOB",
            "channel": None,
            "connection_state": "connected",
            "gsn": "356853050758871",
            "homecarrid": "BOB",
            "hostname": "BOB100133",
            "is_asset": True,
            "is_gps_supported": True,
            "is_upgrade_available": False,
            "is_upgrade_supported": True,
            "ltebandwidth": "20 MHz",
            "mac": None,
            "serial": "356853050758871",
            "service_type": "LTE",
            "ssid": None,
            "summary": "connected",
            "txchannel": "19667",
            "type": "mdm",
            "uid": "f5a8da8f",
            "updated_at": "2018-08-17T11:19:57.019938+00:00",
            "uptime": 86412.8558200002,
        },  
    ]       
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   jezrael    6 年前

    使用 list comprehension dict comprehension 按列名称筛选:

    L

    device_data = [{k: v for k, v in x.items() if k in ['type','id']} for x in L]
    print (device_data)
    [{'id': 474378238, 'type': 'mdm'}, {'id': 5674657356, 'type': 'mdm'}, 
     {'id': 5674657465, 'type': 'mdm'}, {'id': 9756756756, 'type': 'mdm'}]
    
    df = pd.DataFrame(device_data)
    print (df)
               id type
    0   474378238  mdm
    1  5674657356  mdm
    2  5674657465  mdm
    3  9756756756  mdm
    
        2
  •  0
  •   Tarun Kumar Pal    6 年前

    可以通过列标题从数据框中删除这些列来完成此操作。

    例子: p_dev.drop(['Column_1','Column_2','column_2'], axis = 1, inplace = True)

    另一种方法是只在列表中写入所需的列标题,然后覆盖现有的数据帧

    例子:

    pôdef=pôdef[列列表]