我当前正在使用以下命令创建数据框
getting data from url
...
devices = get_device_data.json()
device_data = devices["data"]
p_dev = pd.DataFrame(device_data)
但是json字典中有60列,我只希望其中两列,有没有一种方法可以指定在创建数据框时要包括哪些列,或者我可以以任何方式达到预期的结果?
谢谢
编辑:一些示例数据删除了某些列。我实际上只想要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,
},
]
答案 0 :(得分:1)
使用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
答案 1 :(得分:0)
您可以通过使用列标题从数据框中删除那些列来实现。
示例:p_dev.drop(['Column_1','Column_2','column_2'], axis = 1, inplace = True)
另一种方法是只将列表中需要的列标题写入列表,然后覆盖现有数据框
示例:
col_list = ['Column_1','Column_3']
p_def = p_def [col_list]