从数据框中提取dtypes并映射到标头

时间:2017-07-02 09:04:51

标签: python json pandas dataframe

我需要将json返回到前端,了解数据框的列标题及其数据类型。我试过了:

return {"column header" : header, "filename" : newFilename, 
        "data": result.head(n= rowsLimit).values.tolist(), 
        "datatype": result.dtypes.tolist()}

result.dtypes.tolist(),我收到dtype('int64') is not JSON serializable错误。

由于

2 个答案:

答案 0 :(得分:1)

将它们转换为字符串是一种选择:

>>> json.dumps({"datatype": [str(d) for d in result.dtypes.tolist()]})
'{"datatype": ["float64", "float64", "float64", "float64"]}'

或者,您可以使用repr

>>> json.dumps({"datatype": [repr(d) for d in result.dtypes.tolist()]})
'{"datatype": ["dtype(\'float64\')", "dtype(\'float64\')", "dtype(\'float64\')", "dtype(\'float64\')"]}'

答案 1 :(得分:1)

数据类型具有$(document).ready(function () { $("#jquery_jplayer_1").jPlayer({ ready: function () { $(this).jPlayer("setMedia", { title: "Bubble", mp3: "http://jplayer.org/audio/mp3/Miaow-07-Bubble.mp3" }); }, timeupdate: function(event) { $("#jp_container_1 .jp-ball").css("left",event.jPlayer.status.currentPercentAbsolute + "%"); }, swfPath: "https://rawgit.com/happyworm/jPlayer/tree/master/dist/jplayer", supplied: "mp3", wmode: "window", useStateClassSkin: true, autoBlur: false, smoothPlayBar: true, keyEnabled: true, remainingDuration: true, toggleDuration: true }); /* Modern Seeking */ var timeDrag = false; /* Drag status */ $('.jp-play-bar').mousedown(function (e) { timeDrag = true; updatebar(e.pageX); }); $(document).mouseup(function (e) { if (timeDrag) { timeDrag = false; updatebar(e.pageX); } }); $(document).mousemove(function (e) { if (timeDrag) { updatebar(e.pageX); } }); //update Progress Bar control var updatebar = function (x) { var progress = $('.jp-progress'); var maxduration = $("#jquery_jplayer_1").jPlayer.duration; //audio duration console.log(maxduration); var position = x - progress.offset().left; //Click pos var percentage = 100 * position / progress.width(); //Check within range if (percentage > 100) { percentage = 100; } if (percentage < 0) { percentage = 0; } $("#jquery_jplayer_1").jPlayer("playHead", percentage); //Update progress bar and video currenttime $('.jp-ball').css('left', percentage+'%'); $('.jp-play-bar').css('width', percentage + '%'); $("#jquery_jplayer_1").jPlayer.currentTime = maxduration * percentage / 100; }; }); 属性,可用于返回字符串:

name