I have a data frame (transfers) that I need to filter based on its comparison with another data frame (make_costs).
我有一个数据帧(传输),需要根据它与另一个数据帧(Make_Costs)的比较进行过滤。
transfers:
转账:
make_costs:
制造成本(_C):
Here is what I need:
Remove SourceEquipmentID and DestinationEquipmentID combinations when TransferType is 'Make' or 'MakeTransport' and a combination of SourceEquipmentID x DestinationEquipmentID is not found in make_costs.
以下是我需要的:当TransferType为‘Make’或‘MakeTransport’并且在Make_Costs中找不到SourceEquipmentID x DestinationEquipmentID的组合时,删除SourceEquipmentID和DestinationEquipmentID组合。
Here is my code:
以下是我的代码:
mask = ((~transfers['TransferType'].isin(['Make', 'MakeTransport'])) | (transfers[['SourceEquipmentID', 'DestinationEquipmentID']] <= make_costs[['SourceEquipmentID', 'DestinationEquipmentID']].values))
transfers1 = transfers[mask]
I am getting the following error:
我收到以下错误:
236 mask = ((~transfers['TransferType'].isin(['Make', 'MakeTransport'])) |
--> 237 (transfers[['SourceEquipmentID', 'DestinationEquipmentID']] <= make_costs[[
238 'SourceEquipmentID', 'DestinationEquipmentID']].values))
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/ops/common.py:81, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
77 return NotImplemented
79 other = item_from_zerodim(other)
---> 81 return method(self, other)
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/arraylike.py:52, in OpsMixin.le(self, other)
50 @unpack_zerodim_and_defer("le")
51 def le(self, other):
---> 52 return self._cmp_method(other, operator.le)
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/frame.py:7442, in DataFrame._cmp_method(self, other, op)
7439 def _cmp_method(self, other, op):
7440 axis: Literal1 = 1 # only relevant for Series other case
-> 7442 self, other = ops.align_method_FRAME(self, other, axis, flex=False, level=None)
7444 # See GH#4537 for discussion of scalar op behavior
7445 new_data = self._dispatch_frame_op(other, op, axis=axis)
文件~/anaconda3/lib/python3.10/site-packages/pandas/core/arraylike.py:52,in OpsMixin.le(SELF,OTHER)50@UNPACK_ZEROODIM_AND_DEFER(“LE”)51 def LE(SELF,OTHER):->52返回SELF。_CMP_METHOD(OTHER,OPERATOR.LE)数据帧中的文件~/anaconda3/lib/python3.10/site-packages/pandas/core/frame.py:7442,_CMP_METHOD(SELF,OTHER,OP)7439DEF_CMP_METHOD(SELF,OTHER,操作):7440轴:文字1=1#仅与系列其他案例相关->7442自身,其他=操作。ALIGN_METHOD_FRAME(SELF,OTHER,AXIS,FLEX=FALSE,LEVEL=NONE)7444#有关标量操作行为的讨论,请参阅GH#4537。7445 NEW_DATA=SELF。_DISPATION_FRAME_OP(OTHER,OP,AXIS=AXIS)
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/ops/__init__.py:288, in align_method_FRAME(left, right, axis, flex, level)
285 right = to_series(right[0, :])
287 else:
--> 288 raise ValueError(
289 "Unable to coerce to DataFrame, shape "
290 f"must be {left.shape}: given {right.shape}"
291 )
293 elif right.ndim > 2:
294 raise ValueError(
295 "Unable to coerce to Series/DataFrame, "
296 f"dimension must be <= 2: {right.shape}"
297 )
ValueError: Unable to coerce to DataFrame, shape must be (81, 2): given (6, 2)
Both data frames have different shapes by design. I am trying to eliminate from "transfers" all combinations SourceEquipID x DestEquipID that do not exist in "make_costs" data frame
这两个数据框在设计上具有不同的形状。我正在尝试从“Transfer”中删除不存在于“Make_Costs”数据框中的所有组合SourceEquipID x DestEquipID
Please, help!!
拜托,救命!!
更多回答
I solved it in the following way (using concatenation and isin():
我用以下方式解决了这个问题(使用串联和ISIN():
transfers['key'] = transfers['SourceEquipmentID'] + transfers['DestinationEquipmentID']
mlist = set(make_costs['SourceEquipmentID'] + make_costs['DestinationEquipmentID'])
transfers = transfers[((transfers.TransferType.isin(['Make', 'MakeTransport'])) &
(transfers['key'].isin(mlist))) | transfers.TransferType.isin(['Buy', 'Transport'])]
transfers.drop('key', axis=1, inplace=True)
更多回答
我是一名优秀的程序员,十分优秀!