"The truth value of a Series is ambiguous. " 系列与元素功能

"The truth value of a Series is ambiguous. " Series vs Element Fuction

我有一个数据框,我编写了以下函数来填充新列:

df = pd.DataFrame(np.random.randn(10, 2), columns=['a', 'b'])

def perc(a,b):

    if a/b < 0:
        n = 0
    elif a/b > 1:
        n = 1
    else:
        n = a/b
    return n

df['c']=perc(df['a'],df['b'])

df[1:10]

它应该计算百分比列。这是我收到的错误:

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

我知道这与 dif 和 unc 是系列而不是单个元素有关。但是我该如何解决呢?

您实际要求的内容有点难以用语言描述,但以下示例将其表达出来:

If a is the series [-1, 1, 3, 5] and b is [2, 2, 3, 3], then a/b will be a series like [-0.5, 0.5, 1, 1.6666667], and what you ultimately want to return is [0, 0.5, 1, 1].

您可以 "cap values at 1" 将系列中的最小值与所有系列中的最小值相结合。类似地,您可以通过采用全零系列的系列的最大值来确保没有任何东西低于 0。 numpy 让您轻松做到这一点:

def perc(a,b):
    length = len(a)
    return np.maximum(np.minimum(np.ones(length), a/b), np.zeros(length))

这个有内置方法clip:

In [134]:
df = pd.DataFrame(np.random.randn(10, 2), columns=['a', 'b'])
df

Out[134]:
          a         b
0  0.676248 -0.320346
1 -1.344982  2.170232
2 -0.150036 -1.606179
3  0.350467  0.386958
4  0.551379 -0.378882
5 -0.283632 -1.559516
6  0.266356 -0.859321
7  0.188118  1.275342
8  0.109570  0.546783
9  0.917231 -0.339878

In [136]:
df['c'] = (df['a']/df['b']).clip(lower=0, upper=1)
df

Out[136]:
          a         b         c
0  0.676248 -0.320346  0.000000
1 -1.344982  2.170232  0.000000
2 -0.150036 -1.606179  0.093412
3  0.350467  0.386958  0.905699
4  0.551379 -0.378882  0.000000
5 -0.283632 -1.559516  0.181872
6  0.266356 -0.859321  0.000000
7  0.188118  1.275342  0.147504
8  0.109570  0.546783  0.200390
9  0.917231 -0.339878  0.000000