簡體   English   中英

不正確,Python與R類似

[英]Not True, Python similar to R

有沒有下面類似於R的不錯的解決方案的Python解決方案?

# R
set.seed(1245)
array_truth <- sample(c(T, F), 10, replace = T)
array_int <- 1:10

# get the integers with False index 
> array_int[!array_truth] 

[1] 1 2 4

在R中,您可以使用! 否定,但是我在Python中還沒有找到一個很好的解決方案:

# python
string_data = pd.Series(['aardvark', 'artichoke', np.nan, 'avocado'])
null_values = string_data.isnull()
null_values

0    False
1    False
2     True
3    False
dtype: bool

我知道的最Pythonic解決方案是:

string_data[null_values != True]

0     aardvark
1    artichoke
3      avocado
dtype: object

如果這是我能做的最好的事,那很好,但是我是Python的新手,並且從未在任何地方看到這個特定的問題。

您必須使用~而不是!

>>> string_data[~string_data.isnull()]
0     aardvark  
1    artichoke
3      avocado
dtype: object

正如@SethMMorton在注釋中指出的那樣,邏輯否定通常是在not普通Python中完成的,例如not True返回False ~按位NOT運算符 pandas重載~表示廣播邏輯not僅在這些特定實例中出現,因為Python不允許重載not

暫無
暫無

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

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