新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
pandas.DataFrame.to_json按行轉(zhuǎn)json的方法-創(chuàng)新互聯(lián)
最近需要將csv文件轉(zhuǎn)成DataFrame并以json的形式展示到前臺(tái),故需要用到Dataframe的to_json方法
to_json方法默認(rèn)以列名為鍵,列內(nèi)容為值,形成{col1:[v11,v21,v31…],col2:[v12,v22,v32],…}這種格式,但有時(shí)我們需要按行來(lái)轉(zhuǎn)為json,形如這種格式[row1:{col1:v11,col2:v12,col3:v13…},row2:{col1:v21,col2:v22,col3:v23…}]
通過(guò)查找官網(wǎng)我們可以看到to_json方法有一個(gè)參數(shù)為orient,其參數(shù)說(shuō)明如下:
orient : string Series default is ‘index' allowed values are: {‘split','records','index'} DataFrame default is ‘columns' allowed values are: {‘split','records','index','columns','values'} The format of the JSON string split : dict like {index -> [index], columns -> [columns], data -> [values]} records : list like [{column -> value}, … , {column -> value}] index : dict like {index -> {column -> value}} columns : dict like {column -> {index -> value}} values : just the values array table : dict like {‘schema': {schema}, ‘data': {data}} describing the data, and the data component is like orient='records'. Changed in version 0.20.0
分享名稱:pandas.DataFrame.to_json按行轉(zhuǎn)json的方法-創(chuàng)新互聯(lián)
鏈接URL:http://www.dlmjj.cn/article/idhjo.html