基于pandas Series值列表进行递归

时间:2016-04-07 17:44:50

标签: python pandas recursion

首先,如果我的问题有点模糊,我必须道歉。它是较长问题的简化版本。使用玩具示例,我有一个定义如下的递归函数:

Recur(x,z):
    if stopping criterion: 
      ...
    else:
        S = (some output in pd.Series type)
        if S == 0:
            return Recur(x,z)
        else:
            return Recur(y,z)

基本上S包含一系列1&0和3的熊猫系列。并且根据S的第i个元素是1还是0,执行适当的递归。对于S中的每个元素重复整个递归树。运行此函数会出现以下错误:

  *File "<ipython-input-92-05fb4b8bad66>", line 12, in Recur
    if split_feature_value == 0:
  File "C:\Users\dp\Anaconda3_2.5.0\lib\site-packages\pandas\core\generic.py", line 731, in __nonzero__.format(self.__class__.__name__))*

*ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().*

理想情况下,我想将此作为我可以应用于DataFrames的lambda函数,因此我认为索引是一个潜在的修复是不可能的。

我可以获得一些帮助吗?

1 个答案:

答案 0 :(得分:0)

尝试通过以下方式进行比较:

x_bar.addOrderRule('Reviewer Group', false); //Doesn't Work and now Order is Wrong
x_bar.addOrderRule(['Order','Gender'], false); //Doesn't Work and now Order is Wrong
x_bar.addOrderRule(['Order','Female', 'Male'], false); //Doesn't Work and now Order is Wrong

//passing a function


var ordering = function(x,y){
    console.log(x);
    console.log(y);
};

x_bar.addOrderRule(ordering, false);

// Obj looks like:
// {'Group Bins': '0-10', Gender: [ 'Male', 'Female'], Rate:[0.14, 0.14, 0.30, 0.30], Order: [1, 1]}