python特征工程插件_在Python中使用Pandas进行特征工程,每次计算使用多行

我有以下格式的CSV数据:+-----------------+--------+-------------+

| reservation_num | rate | guest_name |

+-----------------+--------+-------------+

| B874576 | 169.95 | Bob Smith |

| H786234 | 258.95 | Jane Doe |

| H786234 | 258.95 | John Doe |

| F987354 | 385.95 | David Jones |

| N097897 | 449.95 | Mark Davis |

| H567349 | 482.95 | Larry Stein |

| N097897 | 449.95 | Sue Miller |

+-----------------+--------+-------------+

我想在DataFrame中添加一个名为“rate_per_person”的功能(列)。计算方法是将某一特定预订号码的价格除以与他们入住期间有相同预订号码的客人总数。在

这是我的代码:

^{pr2}$

以及错误消息:Traceback (most recent call last):

File "", line 2, in

row['rate_per_person'] = row['rate'] / ds[row['reservation_num']].count

File "/Users//anaconda/lib/python3.6/site-packages/pandas/core/frame.py", line 2062, in __getitem__

return self._getitem_column(key)

File "/Users//anaconda/lib/python3.6/site-packages/pandas/core/frame.py", line 2069, in _getitem_column

return self._get_item_cache(key)

File "/Users//anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 1534, in _get_item_cache

values = self._data.get(item)

File "/Users//anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 3590, in get

loc = self.items.get_loc(item)

File "/Users//anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2395, in get_loc

return self._engine.get_loc(self._maybe_cast_indexer(key))

File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239)

File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)

File "pandas/_libs/hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405)

File "pandas/_libs/hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359)

KeyError: 'B874576'

根据错误消息,很明显最后一行代码的ds[row['reservation_num']].count部分有问题。但是,我不确定如何正确地获取每个预订的客人数量,从而使我能够以编程方式创建新功能。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值