如何使用一列的值来访问另一列中的值?

时间:2018-02-15 22:12:28

标签: pandas

如何使用一列的值来访问另一列的值

[{"id":"7b4017ce.0802d8","type":"inject","z":"76a6ae8d.8a35","name":"red/white/blue","topic":"","payload":"[{\"x\":100,\"y\":100,\"h\":40,\"w\":50,\"c\":\"red\"},{\"x\":200,\"y\":200,\"h\":20,\"w\":75,\"c\":\"white\"},{\"x\":300,\"y\":300,\"h\":20,\"w\":75,\"c\":\"blue\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":540,"wires":[["3adb8b4a.dbf3c4"]]},{"id":"3adb8b4a.dbf3c4","type":"ui_template","z":"76a6ae8d.8a35","group":"f2c8d56c.4d75b8","name":"D3 template","order":1,"width":0,"height":0,"format":"<style>\n    #myBoxes {\n        height: 800px;\n        width: 800px;\n    }\n</style>\n\n<svg id=\"myBoxes\">\n    <!-- svg content goes here -->\n</svg>\n\n<script>\n(function(scope) { \n    // watch for msgs from node-red\n    scope.$watch('msg.payload', function(payload) {\n        drawBoxes(\"myBoxes\", payload);\n    })\n})(scope);\n\nfunction drawBoxes(id, data) {\n    var svg = d3.select(\"#\" + id);\n    var rect = svg.selectAll(\"rect\")\n        .data(data);\n\n    rect.enter()\n        .append(\"rect\");\n    \n    rect.attr(\"x\", function(d) { return d.x; })\n        .attr(\"y\", function(d) { return d.y; }) \n        .attr(\"width\", function(d) { return d.w; })\n        .attr(\"height\", function(d) { return d.h; })\n        .style(\"fill\", function(d) { return d.c; });\n    \n    rect.exit()\n        .remove();\n}\n</script>","storeOutMessages":false,"fwdInMessages":false,"templateScope":"local","x":510,"y":540,"wires":[[]]},{"id":"e1ab2277.a8f19","type":"ui_template","z":"76a6ae8d.8a35","group":"d215108d.8b51b","name":"D3 libraries","order":0,"width":0,"height":0,"format":"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.2/d3.min.js\"></script>\n<script src=\"https://rawgit.com/gka/d3-jetpack/master/d3-jetpack.js\"></script>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"global","x":510,"y":500,"wires":[[]]},{"id":"a4d0e22c.4689","type":"inject","z":"76a6ae8d.8a35","name":"black/white","topic":"","payload":"[{\"x\":100,\"y\":100,\"h\":40,\"w\":40,\"c\":\"black\"},{\"x\":200,\"y\":200,\"h\":40,\"w\":40,\"c\":\"white\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":580,"wires":[["3adb8b4a.dbf3c4"]]},{"id":"e7af594f.9862d8","type":"inject","z":"76a6ae8d.8a35","name":"red/yellow/green","topic":"","payload":"[{\"x\":100,\"y\":100,\"h\":10,\"w\":50,\"c\":\"red\"},{\"x\":200,\"y\":200,\"h\":20,\"w\":15,\"c\":\"yellow\"},{\"x\":300,\"y\":300,\"h\":60,\"w\":80,\"c\":\"green\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":500,"wires":[["3adb8b4a.dbf3c4"]]},{"id":"f2c8d56c.4d75b8","type":"ui_group","z":"","name":"D3","tab":"e3e3a35c.7a77f","order":2,"disp":true,"width":"12","collapse":false},{"id":"d215108d.8b51b","type":"ui_group","z":"","name":"Recalls","tab":"3c000bae.098b94","order":2,"disp":true,"width":"18"},{"id":"e3e3a35c.7a77f","type":"ui_tab","z":"","name":"Libraries","icon":"fa-bank","order":6},{"id":"3c000bae.098b94","type":"ui_tab","z":"","name":"Vehicles","icon":"fa-car"}]

所以如何获取价值&#39; bleh&#39;每一行?

import numpy
impot pandas

numpy.random.seed(123)
df = pandas.DataFrame((numpy.random.normal(0, 1, 10)), columns=[['Value']])
df['bleh'] = df.index.to_series().apply(lambda x: numpy.random.randint(0, x + 1, 1)[0])

编辑:

感谢@ScottBoston。我的DF构造函数有一层df.Value.iloc[df['bleh']] 太多了。 正确答案是:

[]

1 个答案:

答案 0 :(得分:1)

尝试:

df['Value'].tolist()

输出:

[-1.0856306033005612,
 0.9973454465835858,
 0.28297849805199204,
 -1.506294713918092,
 -0.5786002519685364,
 1.651436537097151,
 -2.426679243393074,
 -0.42891262885617726,
 1.265936258705534,
 -0.8667404022651017]

您的数据框构造函数仍需要修复。

您在寻找:

df.set_index('bleh')

输出:

         Value
bleh          
0    -1.085631
1     0.997345
2     0.282978
1    -1.506295
4    -0.578600
0     1.651437
0    -2.426679
4    -0.428913
1     1.265936
7    -0.866740

如果是这样,您的数据框构造函数中包含[]的额外集合。

np.random.seed(123)
df = pd.DataFrame((np.random.normal(0, 1, 10)), columns=['Value'])
df['bleh'] = df.index.to_series().apply(lambda x: np.random.randint(0, x + 1, 1)[0])
数据框中的

columns参数列表不是列表列表。