小编ala*_*ano的帖子

pandas:按二级索引的范围切片MultiIndex

我有一个像这样的MultiIndex系列:

import numpy as np
import pandas as pd

buckets = np.repeat(['a','b','c'], [3,5,1])
sequence = [0,1,5,0,1,2,4,50,0]

s = pd.Series(
    np.random.randn(len(sequence)), 
    index=pd.MultiIndex.from_tuples(zip(buckets, sequence))
)

# In [6]: s
# Out[6]: 
# a  0    -1.106047
#    1     1.665214
#    5     0.279190
# b  0     0.326364
#    1     0.900439
#    2    -0.653940
#    4     0.082270
#    50   -0.255482
# c  0    -0.091730
Run Code Online (Sandbox Code Playgroud)

我想得到s ['b']值,其中第二个索引(' sequence')在2到10之间.

在第一个索引上切片工作正常:

s['a':'b']
# Out[109]: 
# bucket  value
# a       0        1.828176
#         1        0.160496
#         5        0.401985 …
Run Code Online (Sandbox Code Playgroud)

python pandas

24
推荐指数
3
解决办法
3万
查看次数

pymongo mapreduce db断言失败

我刚刚开始使用mongo和map/reduce,并且在使用pymongo时遇到以下错误,直接使用mongo命令行时我没有得到(我意识到这个问题有类似问题,但我的似乎更基本).

我直接从pymongo文档中使用该示例:http://api.mongodb.org/python/1.3%2B/examples/map_reduce.html

from pymongo import Connection
from pymongo.code import Code

db = Connection().map_reduce_example
db.things.insert({"x": 1, "tags": ["dog", "cat"]})
db.things.insert({"x": 2, "tags": ["cat"]})
db.things.insert({"x": 3, "tags": ["mouse", "cat", "dog"]})
db.things.insert({"x": 4, "tags": []})


m = Code("function () {this.tags.forEach(function(z) {emit('d, 1);});}")
m = Code("function () {emit('dog', 1);}")

r = Code("function (key, values) {var total = 0;for (var i = 0; i < values.length; i++) {total += values[i];}return total;}")


result = db.things.map_reduce(m, r)
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

Traceback (most recent call last):
  File "demo.py", …
Run Code Online (Sandbox Code Playgroud)

mapreduce pymongo

3
推荐指数
1
解决办法
1879
查看次数

标签 统计

mapreduce ×1

pandas ×1

pymongo ×1

python ×1