使用lambda并应用函数将另一列添加到我的数据帧

时间:2017-10-04 16:25:07

标签: python pandas dataframe lambda apply

我的数据框如下所示:

>>>df1
                               ARP            CDP
DATE                                             
2017-09-15 03:49:00          -1.81      -1.81
2017-09-15 11:30:00          -2.70      -2.70
2017-09-15 13:15:00          -2.70      -2.70
2017-09-15 16:03:00          -2.70      -2.70


>>>df1.dtypes

ARP    float64
CDP    float64
dtype: object

我有以下function

def lookup(value1, value2):
    print type(value1)
    value1 = round(value1, 5)
    value2 = round(value2, 5)
    if value1 == value2:
        return 0.0
    else:
        diff = abs(value1 - value2)
        if diff == inf:
            return 100.0
        elif math.isnan(diff):
            return 100.0
        else:
            return diff

现在我运行以下内容:

df1['DIFF'] = df1.apply(lambda x: lookup(x[df1.columns[0]], x[df1.columns[1]]), axis=1)

我收到以下错误:

<class 'pandas.core.series.Series'>

Traceback (most recent call last):
    df1['DIFF'] = df1.apply(lambda x: lookup(x[df1.columns[0]], 
x[df1.columns[1]]), axis=1)
  File blah, line 3718, in apply
    return self._apply_standard(f, axis, reduce=reduce)
  File "/pandas/core/frame.py", line 3808, in _apply_standard
    results[i] = func(v)
  File "<ipython-input-688-449dea597035>", line 29, in <lambda>
    df1['DIFF'] = df1.apply(lambda x: lookup(x[df1.columns[0]], 
x[df1.columns[1]]), axis=1)
  File "<ipython-input-669-c78026996809>", line 5, in lookup
    if value1 == value2:
  File "/pandas/core/generic.py", line 714, 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().', u'occurred at index 1993-06-04 15:25:00')

无法弄清楚为什么!!理想情况下,我只想创建另一个名为DIFF的列,它将返回0或返回其他两列的差异。

由于

1 个答案:

答案 0 :(得分:0)

尝试np.where

import numpy as np
df['DIFF'] = np.where(df['ARP'] - df['CDP'] == 0,0,abs(df['ARP'] - df['CDP']))
df['DIFF']