簡體   English   中英

如何按多列和條件過濾 pandas 數據幀

[英]how to filter pandas dataframes by multiple columns and conditions

我有以下一組數據:

在此處輸入圖像描述

我正在嘗試使用以下內容過濾結果:

df = df.loc[
    (df['Left In Stock']>10) 
    & (df['Price (£)']>=10) 
    & (df['Weight(g)']<=700) 
    & ~(
        df['Title'].str.contains('the raven|on the') 
        & df['Colour']=='White'
    )
]

我正在努力實現以下結果:

在此處輸入圖像描述

簡而言之,我試圖通過首先刪除所有庫存少於 10 且價格大於或等於 10 英鎊的行來應用 4 個條件來過濾數據庫。 排除重量超過 700g 的行,最后如果標題包含短語“the raven”或“on the”,並且顏色為白色,僅排除這些行。

如果標題包含短語“the raven”或“on the”並且顏色不是白色,那么這些行仍應包含在結果中。

我收到以下錯誤:

Traceback (most recent call last):
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 301, in na_logical_op
    result = op(x, y)
TypeError: unsupported operand type(s) for &: 'bool' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\A\PycharmProjects\pythonProject\AutoTrader.py", line 189, in <module>
    df = df.loc[(df['Left In Stock']>10) & (df['Price (£)']>=10) & (df['Weight(g)']<=700) & ~(df['Title'].str.contains('the raven|on the') & df['Colour']=='White')]
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\common.py", line 70, in new_method
    return method(self, other)
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\arraylike.py", line 70, in __and__
    return self._logical_method(other, operator.and_)
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py", line 5634, in _logical_method
    res_values = ops.logical_op(lvalues, rvalues, op)
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 391, in logical_op
    res_values = na_logical_op(lvalues, rvalues, op)
  File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 308, in na_logical_op
    result = libops.vec_binop(x.ravel(), y.ravel(), op)
  File "pandas\_libs\ops.pyx", line 252, in pandas._libs.ops.vec_binop
  File "pandas\_libs\ops.pyx", line 245, in pandas._libs.ops.vec_binop
TypeError: unsupported operand type(s) for &: 'bool' and 'str'

Process finished with exit code 1

使用.gt().le().ne().eq() etc.可以在讓所有()正確時省去很多麻煩。 以及為清晰起見跨多行格式化代碼:

mask = (df['Left In Stock'].gt(10)
        & df['Price (£)'].ge(10)
        & df['Weight(g)'].le(700)
        & ~(df['Title'].str.contains('the raven|on the')
            & df['Colour'].eq('White')))

out = df.loc[mask]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM