首页 > 解决方案 > 使用 panda 的分配功能时出现 KeyError

问题描述

我在下面有数据框,我希望根据收入和预算创建新变量“profit_loss”和“profit_margin”。

        revenue     budget
0      1513528810  150000000
1       378436354  150000000
2       295238201  110000000
3      2068178225  200000000
4      1506249360  190000000

我尝试使用 pandas assign()方法创建新变量,但出现以下错误。

d.assign(profit_loss = (d['revenue'] - d['budget']), 
         profit_loss_margin = (d['profit_loss'] * 100 / d['revenue']), 
         financial_status = d['profit_loss'].apply(lambda num: 'Profit-Making' if num > 0 else 'Loss- 
         Making'))

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)    2895                 return self._engine.get_loc(casted_key)    2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err    2898     2899         if tolerance is not None:

KeyError: 'profit_loss'

但是,下面的代码工作得很好。

d.assign(profit_loss = (d['revenue'] - d['budget']))

请告知我在以前的代码中是否犯了任何错误?

标签: pythonpython-3.xpandasdataframe

解决方案


您需要lambda像这里一样使用新创建的列profit_loss

df = d.assign(profit_loss = (d['revenue'] - d['budget']), 
              profit_loss_margin = lambda x: (x['profit_loss'] * 100 / x['revenue']), 
              financial_status =  lambda x: x['profit_loss'].apply(lambda num: 'Profit-Making' if num > 0 else 'Loss- Making'))

print (df)
      revenue     budget  profit_loss  profit_loss_margin financial_status
0  1513528810  150000000   1363528810           90.089386    Profit-Making
1   378436354  150000000    228436354           60.363216    Profit-Making
2   295238201  110000000    185238201           62.741949    Profit-Making
3  2068178225  200000000   1868178225           90.329654    Profit-Making
4  1506249360  190000000   1316249360           87.385887    Profit-Making

推荐阅读