簡體   English   中英

如何根據pandas數據框中的多列值條件排除行?

[英]How to exclude rows based on multi column value conditions in pandas dataframe?

下面是熊貓數據框廣告:

email             score
a@domain.com      A
b@domain.com      A
c@domain.com      C
d@domain.com      B

我想排除帶有email a@domain.comc@domain.com行。預期結果如下:

email            score
b@domain.com      A
d@domain.com      B

我嘗試了 3 次但失敗了:

df=df[df.email !='a@domain.com' & df.email !='c@domain.com' ]
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]


df=df[df.email !='a@domain.com' && df.email !='c@domain.com' ]
SyntaxError: invalid syntax


df=df[df.email !='a@domain.com' | 'c@domain.com' ]
TypeError: unsupported operand type(s) for |: 'str' and 'str'

有什么問題?

你必須用括號括起來:

df = df[(df.email != 'a@domain.com') & (df.email != 'c@domain.com')]

也就是說,使用isin會更容易:

df = df[~df.email.isin(['a@domain.com', 'c@domain.com'])]

現在:

print(df)

將是預期的輸出。

暫無
暫無

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

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