简体   繁体   English

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

[英]pandas - creating data frame from json data, specific which columns to include

I am currently creating my data frame with the below commands我目前正在使用以下命令创建我的数据框

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

However the json dictionary has 60 columns in, of which I only want two of them, is there a way to specify which columns to include when creating a data frame, or any way I can achieve the desired result?但是 json 字典有 60 列,其中我只想要其中的两列,有没有办法指定在创建数据框时要包含哪些列,或者我可以通过什么方式获得所需的结果?

Thanks谢谢

EDIT: some sample data with some columns deleted.编辑:删除了一些列的一些示例数据。 I actually only want the id and hostname columns我实际上只想要 id 和 hostname 列

[
    {
        "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,
    },  
]       

Use list comprehension with dict comprehension for filtering by colums names:使用list comprehensiondict comprehension来按列名称过滤:

L is list of input data 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

You can do it by deleting those columns from the data frame by the column header.您可以通过列标题从数据框中删除这些列来实现。

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

Another Way to do it to write only those column headers in a list which are needed and then overwrite the existing dataframe另一种方法是只在列表中写入那些需要的列标题,然后覆盖现有的数据框

Example:例子:

col_list = ['Column_1', 'Column_3'] col_list = ['Column_1', 'Column_3']

p_def=p_def[col_list] p_def=p_def[col_list]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM