![](/img/trans.png)
[英]Remove rows when the occurrence of a column value in the data frame is less than a certain number using pandas/python?
[英]Remove rows in python less than a certain value
我覺得這個問題之前肯定有人回答過,但是我在stack overflow上找不到答案!
我有一個 dataframe result
,看起來像這樣,我想刪除所有小於或等於10 的值
>>> result
Name Value Date
189 Sall 19.0 11/14/15
191 Sam 10.0 11/14/15
192 Richard 21.0 11/14/15
193 Ingrid 4.0 11/14/15
此命令有效並刪除所有 10 的值:
df2 = result[result['Value'] != 10]
但是當我嘗試添加 <= 限定符時,我收到錯誤消息SyntaxError: invalid syntax
df3 = result[result['Value'] ! <= 10]
我覺得可能有一個非常簡單的解決方案。
而不是這個
df3 = result[result['Value'] ! <= 10]
用
df3 = result[~(result['Value'] <= 10)]
它會起作用。 或者簡單地使用
df3 = result[result['Value'] > 10]
python 不使用!
否定。 它使用not
。 看到這個答案
在這個特定的例子中!=
是一個兩個字符的字符串,表示not equal
。 這不是==
的否定。
選項1
這應該有效,除非你有NaN
result[result['Value'] > 10]
選項 2
使用一元運算符~
來否定布爾系列
result[~(result['Value'] <= 10)]
我還有一個建議,可能會有所幫助
df3 = result.drop(result[result['Value'] < 10].index, inplace = True)
還有df.query()
:
result.query('Value > 10')
Name Value Date
189 Sall 19.0 11/14/15
191 Sam 10.0 11/14/15
192 Richard 21.0 11/14/15
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.