一例tornado框架下利用python panda对数据进行crud操作
- get提交部分
<script> /* $("#postbtn").click(function () { $.ajax({ url:'/loaddata', datatype:'json', data:{"filename":$("#excelfile").val()}, success:function (data) { console.log(JSON.parse(data)) } }) })*/ $("#postbtn").click(function () { $.get('/loaddata',{'filename':$("#excelfile").val()},function (data) {
//ajax get请求的装逼写法$.get('响应请求的url',请求的数据,回调函数) console.log(data) },'json') }) </script>
- 相关RequestHandler
class loaddata(RequestHandler): def get(self): wenwa = self.get_argument('filename') client = MongoClient("localhost",27017) db = client['olddream'] //选取数据库 cols = db['bloodtype']//选取集合 data = DataFrame(list(cols.find()))//读取记录 print(data) rets = data.to_json(orient="records")//把dataframe类型转为json格式数据 self.write(json.dumps(rets))
- 前端response数据形式(orient=“records”)
[{"_id":0,"bloodtype":"A"}, {"_id":1,"bloodtype":"B"}, {"_id":2,"bloodtype":"AB"}, {"_id":3,"bloodtype":"O"}, {"_id":4,"bloodtype":"RH"}]
- DataFrame类型转为其他格式的方法
split,样式为 {index -> [index], columns -> [columns], data -> [values]} records,样式为[{column -> value}, … , {column -> value}] index ,样式为 {index -> {column -> value}} columns,样式为 {index -> {column -> value}} values,数组样式 table,样式为{‘schema’: {schema}, ‘data’: {data}},和records类似
下列部分引自
作者:huanbia
来源:CSDN
原文:https://blog.csdn.net/huanbia/article/details/72674832
大致效果
df = pd.DataFrame([['a', 'b'], ['c', 'd']],
index=['row 1', 'row 2'],
columns=['col 1', 'col 2'])
###########
split
###########
df.to_json(orient='split')
>'{"columns":["col 1","col 2"],
"index":["row 1","row 2"],
"data":[["a","b"],["c","d"]]}'
###########
index
###########
df.to_json(orient='index')
>'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
###########
records
###########
df.to_json(orient='records')
>'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
###########
table
###########
df.to_json(orient='table')
>'{"schema": {"fields": [{"name": "index", "type": "string"},
{"name": "col 1", "type": "string"},
{"name": "col 2", "type": "string"}],
"primaryKey": "index",
"pandas_version": "0.20.0"},
"data": [{"index": "row 1", "col 1": "a", "col 2": "b"},
{"index": "row 2", "col 1": "c", "col 2": "d"}]}'