Python Pandas删除DataFrame两列值条件中的行

时间:2019-10-29 23:35:43

标签: python pandas dataframe

我有一个数据框,我想根据2个列值条件删除行。如果Product_Name ='Access Point',并且Contract_No ='No_Contract',则删除数据框的行。

我尝试了两种方法。

方法1

dfdata_inv.drop(dfdata_inv[dfdata_inv['Product_Name'] ==  'Access Point' & dfdata_inv['Contract_No'] ==  'No_Contract'].index, inplace = True)

结果1

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in na_op(x, y)
   1788         try:
-> 1789             result = op(x, y)
   1790         except TypeError:

/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in rand_(left, right)
    186 def rand_(left, right):
--> 187     return operator.and_(right, left)
    188 

TypeError: unsupported operand type(s) for &: 'str' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in na_op(x, y)
   1803                 try:
-> 1804                     result = libops.scalar_binop(x, y, op)
   1805                 except (TypeError, ValueError, AttributeError,

pandas/_libs/ops.pyx in pandas._libs.ops.scalar_binop()

/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in rand_(left, right)
    186 def rand_(left, right):
--> 187     return operator.and_(right, left)
    188 

TypeError: unsupported operand type(s) for &: 'bool' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-34-c4842896bbf6> in <module>
----> 1 dfdata_inv.drop(dfdata_inv[dfdata_inv['Product_Name'] ==  'Access Point' & dfdata_inv['Contract_No'] ==  'No_Contract'].index, inplace = True)

/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in wrapper(self, other)
   1848         filler = (fill_int if is_self_int_dtype and is_other_int_dtype
   1849                   else fill_bool)
-> 1850         res_values = na_op(self.values, ovalues)
   1851         unfilled = self._constructor(res_values,
   1852                                      index=self.index, name=res_name)

/anaconda3/lib/python3.7/site-packages/pandas/core/ops.py in na_op(x, y)
   1808                                     "with a scalar of type [{typ}]"
   1809                                     .format(dtype=x.dtype,
-> 1810                                             typ=type(y).__name__))
   1811 
   1812         return result

TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]

方法2

dfdata_inv.drop(dfdata_inv[dfdata_inv['Product_Name'] ==  'Access Point' and dfdata_inv['Contract_No'] ==  'No_Contract'].index, inplace = True)

结果2

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-689996a56e8b> in <module>
----> 1 dfdata_inv.drop(dfdata_inv[dfdata_inv['Product_Name'] ==  'Access Point' and dfdata_inv['Contract_No'] ==  'No_Contract'].index, inplace = True)

/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1476         raise ValueError("The truth value of a {0} is ambiguous. "
   1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478                          .format(self.__class__.__name__))
   1479 
   1480     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我可以使用以下一种条件轻松地删除一行:

dfdata_inv.drop(dfdata_inv[dfdata_inv['Product_Name'] ==  'Access Point'

1 个答案:

答案 0 :(得分:0)

代替布尔值,只需使用布尔掩码并分配一个新的df:

new_df = dfdata_inv[(dfdata_inv['Product_Name']=='Access Point')&(dfdata_inv['Contract_No']=='No_Contract')]