运算符中的python相当于什么?我试图通过仅在行中的列具有在我的列表中找到的值时保留行来过滤掉pandas数据库。
我尝试使用任何(),并且我有很大的困难。
与R docs的熊猫比较是here。
s <- 0:4
s %in% c(2,4)
isin()方法类似于%operator中的R%:
In [13]: s = pd.Series(np.arange(5),dtype=np.float32)
In [14]: s.isin([2, 4])
Out[14]:
0 False
1 False
2 True
3 False
4 True
dtype: bool
FWIW:无需调用大熊猫,这里是使用纯蟒蛇中的for loop
和list compression
的答案
x = [2, 3, 5]
y = [1, 2, 3]
# for loop
for i in x: [].append(i in y)
Out: [True, True, False]
# list comprehension
[i in y for i in x]
Out: [True, True, False]
简而言之,Python中的等价于R运算符%in%。
例如,这是为了检查股票代码“CHDN.OQ”是否在pandas数据框的'assetCode'列中:
print("CHDN.OQ" in market_train_df['assetCode'])
该表达式的结果为True of False。
正如其他人指出的那样,基础Python的in
运算符效果很好。
myList = ["a00", "b000", "c0"]
"a00" in myList
# True
"a" in myList
# False