从pandas数据框中提取类似JSON的数据到单独的列

时间:2019-11-13 14:31:54

标签: python json pandas

以下是示例数据:

{"Navbar":{"appId":"metaManagement","masked":true},"control":{"control":"accountNumber","id":"6789","value":null,"masked":false}}

{"Navbar":{"appId":"metaManagement","masked":true},"control":{"control":"accountNumber","id":"12345","value":null,"masked":false}}

我搜索了许多博客和平台,但没有合适的解决方案。我可以将appID提取到单独的列中吗?请帮忙。

2 个答案:

答案 0 :(得分:1)

您可以尝试一下,

>>> from pandas.io.json import json_normalize
>>> d = {"Navbar":{"appId":"metaManagement","masked":"true"},"control":{"control":"accountNumber","id":"6789","value":"null","masked":"false"}}
>>> json_normalize(d)
     Navbar.appId Navbar.masked control.control control.id control.value control.masked
0  metaManagement          true   accountNumber       6789          null          false

答案 1 :(得分:0)

如果数据在pandas dataframe列中,则另一种方法。

@RestController
public class TestController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value="/hello")
    public PayloadRespons home(HttpServletRequest httpRequest, Model model) { 


   PayloadRespons payload = new PayloadRespons();
   payload.setAuthorization = "test string"
           // model.addAttribute("Authorization", "test string");

            return payload;
    }  
}

class PayloadRespons{

private String Authorization;

.... some more which you have 

.... Getter and Settter 
}