Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python isin、str.contains和if条件之间的差异?_Python_Dataframe - Fatal编程技术网

Python isin、str.contains和if条件之间的差异?

Python isin、str.contains和if条件之间的差异?,python,dataframe,Python,Dataframe,我通常不知道是否要用某些东西过滤数据框列项, 如果df[“column”]中使用了“aa”,则应isin或.str.contains或 请告诉我哪些用于不同的情况?isin 如果要检查序列的值中多个字符串之一的出现情况,请使用isin: import pandas as pd things =

我通常不知道是否要用某些东西过滤数据框列项, 如果df[“column”]中使用了“aa”,则应
isin
.str.contains

请告诉我哪些用于不同的情况?

isin 如果要检查序列的值中多个字符串之一的出现情况,请使用
isin

import pandas as pd                                                                                                      
things = pd.Series(['apple', 'banana', 'house', 'car'])                                                                                                                              
fruits = ['apple', 'banana', 'kiwi']                                                                                                                                            
things.isin(fruits)
输出:

0     True
1     True
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
.str.contains
.str.contains
执行相同的操作,但仅针对一个字符串,并且它还匹配字符串的部分

things.str.contains('apple')
输出:

0     True
1     True
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
输出:

0     True
1     True
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
0     True
1    False
2    False
3    False
dtype: bool
在里面
A系列
检查
A系列
是否在pd系列的索引中。系列:

"apple" in things                                                                                                                                                              
# Output: False
我们的
things
系列的索引中没有“苹果”,原因很快就清楚了:

> things
0     apple
1    banana
2     house
3       car
dtype: object
第一列描述了索引,因此我们可以检查它:

0 in things                                                                                                                                                              
# Output: True

我将尝试通过示例向您展示这些差异:

df = pd.DataFrame({'A': [4,8], 'B': ['hello toto','foo bar']})
df_1 = df[df['B'].str.contains("hello")]
df_2 = df.isin([4, "foo bar", "hello", "hello toto mamamia"])
df_3 = df.loc[df["B"] == "foo bar"] # implicit "if"

# df
   A            B
0  4   hello toto
1  8      foo bar

# df_1
   A           B
0  4  hello toto

# df_2
       A      B
0   True  False
1  False   True

# df_3
   A        B
1  8  foo bar

isin
如果列表中的元素x,则返回True,否则返回False

str.contains
如果字符串中的元素x代表列表中的字符串,则返回True,否则返回False

如果
如果行['string']==df中行的元素x,则返回True,否则返回False

最后一个元素相当于
isin
,在
列表中只有一个元素

基本上,
if
&
isin
是关于检查某列的值是否是列表的一部分,而
str.contains
是关于搜索列中的字符串以查找某个子字符串

用法示例:

df
>>
  clientID   priceType
0  ER12312      member
1  ER24421    standard
2  WB44213      member
3  ER92932  discount15
4  WB02321    standard
我们希望所有客户支付会员或标准价格:

df[df.priceType.isin(['member','standard'])]
>>
  clientID priceType
0  ER12312    member
1  ER24421  standard
2  WB44213    member
4  WB02321  standard
如果我们想要所有的“ER”客户端ID:

df[df.clientID.str.contains('ER')]
>>
  clientID   priceType
0  ER12312      member
1  ER24421    standard
3  ER92932  discount15

第一个是ISIN,第二个是IN。我对这两个问题都有疑问。这不包括
str.contains('some')
如何返回
True
以表示
“某物”