在Pandas中创建条件列时出错

时间:2018-10-10 16:18:09

标签: python pandas dataframe

熊猫新手在这里。我正在尝试非常准确地进行this的工作-我正在使用数据库并导入.csv文件。在文件“成本”中,某些项目的“内容”值,而某些项目的“广告系列”值。看起来像这样:

content     campaign     number
salad       NaN          5
NaN         yogurt       7

&tc。这些项目都没有内容和广告系列的值。

在我的原始数据集p中,我想创建一个新的“数字”列,以查看p中数据点的内容和广告系列列,并使用cost.csv文件中的数字值填充新列。

这是我的代码:

def add_cpp(x):
cppcost = cost.apply(lambda y: y['cpp'] if
                    (x['content'] == y['content']) 
                    or (x['campaign'] == y['campaign'])
                     else None, axis = 1)
cppcost = cost.dropna(axis=0, how='all')
if cppcost.empty:
    cppcost=np.nan
else:
    cppcost = cppcost.to_string(index=False)
return cppcost
p['cpp'] = p.apply(add_cpp, axis=1)

这是错误:

KeyError                                  Traceback (most recent call 
last)
<ipython-input-30-8d415648952c> in <module>()
----> 1 p['cpp'] = p.apply(add_cpp, axis=1)`

/.../lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self,     
func, axis, broadcast, raw, reduce, args, **kwds)
    4875                         f, axis,
    4876                         reduce=reduce,
 -> 4877                         ignore_failures=ignore_failures)
    4878             else:
    4879                 return self._apply_broadcast(f, axis)`

/.../lib/python2.7/site-packages/pandas/core/frame.pyc in 
 _apply_standard(self, func, axis, ignore_failures, reduce)
   4971             try:
   4972                 for i, v in enumerate(series_gen):
-> 4973                     results[i] = func(v)
   4974                     keys.append(v.name)
   4975             except Exception as e:`

<ipython-input-29-bae74713ac34> in add_cpp(x)
      2     cppcost = cost.apply(lambda y: y['cpp'] if
      3                         (x['content'] == y['content'])
----> 4                          else None, axis = 1)
      5     cppcost = cost.dropna(axis=0, how='all')
      6     if cppcost.empty:`

/.../lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, 
 func, axis, broadcast, raw, reduce, args, **kwds)
   4875                         f, axis,
   4876                         reduce=reduce,
-> 4877                         ignore_failures=ignore_failures)
   4878             else:
   4879                 return self._apply_broadcast(f, axis)`

/.../lib/python2.7/site-packages/pandas/core/frame.pyc in 
 _apply_standard(self, func, axis, ignore_failures, reduce)
   4971             try:
   4972                 for i, v in enumerate(series_gen):
-> 4973                     results[i] = func(v)
   4974                     keys.append(v.name)
   4975             except Exception as e:`

<ipython-input-29-bae74713ac34> in <lambda>(y)
      1 def add_cpp(x):
      2     cppcost = cost.apply(lambda y: y['cpp'] if
----> 3                         (x['content'] == y['content'])
      4                          else None, axis = 1)
      5     cppcost = cost.dropna(axis=0, how='all')`

/.../lib/python2.7/site-packages/pandas/core/series.pyc in     
 __getitem__(self, key)
    621         key = com._apply_if_callable(key, self)
    622         try:
--> 623             result = self.index.get_value(self, key)
    624 
    625             if not is_scalar(result):`
/.../lib/python2.7/site-packages/pandas/core/indexes/base.pyc in 
 get_value(self, series, key)
   2572                     raise InvalidIndexError(key)
   2573                 else:
-> 2574                     raise e1
   2575             except Exception:  # pragma: no cover
   2576                 raise e1`

KeyError: ('cpp', u'occurred at index 3', u'occurred at index 1')

非常感谢任何帮助-非常感谢!

0 个答案:

没有答案