Series.str.contains(pat,case = True,flags = 0,na = nan,regex = True)
# Returning a Series of booleans using only a literal pattern.
s1 = pd.Series(['Mouse', 'dog', 'house and parrot', '23', np.nan])
s1.str.contains('og', regex=False)
0 False
1 True
2 False
3 False
4 NaN
dtype: object
#Returning an Index of booleans using only a literal pattern.
ind = pd.Index(['Mouse', 'dog', 'house and parrot', '23.0', np.nan])
ind.str.contains('23', regex=False)
Index([False, False, False, True, nan], dtype='object')
# case区分大小写,regex使用正则
s1.str.contains('oG', case=True, regex=True)
0 False
1 False
2 False
3 False
4 NaN
dtype: object
# nan值判定为False
s1.str.contains('og', na=False, regex=True)
0 False
1 True
2 False
3 False
4 False
dtype: bool
# 使用正则
s1.str.contains('house|dog', regex=True)
0 False
1 True
2 True
3 False
4 NaN
dtype: object
# Ignoring case sensitivity using flags with regex.
import re
s1.str.contains('PARROT', flags=re.IGNORECASE, regex=True)
0 False
1 False
2 True
3 False
4 NaN
dtype: object
df = pd.DataFrame({'name': ['悟空','唐僧','八戒','沙僧','白龙马'],
'age': [1000,33,800,700,500],
'兵器': ['金箍棒',np.nan, '九齿钉耙','降妖宝杖','龙泉宝剑'],
'力量':[100.0, 10.0, 60.0, 50.0, np.nan]},
index=['a', 'b', 'c', 'd','e'])
df
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | 悟空 | 1000 | 金箍棒 | 100.0 |
b | 唐僧 | 33 | NaN | 10.0 |
c | 八戒 | 800 | 九齿钉耙 | 60.0 |
d | 沙僧 | 700 | 降妖宝杖 | 50.0 |
e | 白龙马 | 500 | 龙泉宝剑 | NaN |
# 当值为列表时,检查DataFrame中的每个值是否都存在于列表中
df.isin([1000, 33])
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | False | True | False | False |
b | False | True | False | False |
c | False | False | False | False |
d | False | False | False | False |
e | False | False | False | False |
~df.isin([1000, 33])
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | True | False | True | True |
b | True | False | True | True |
c | True | True | True | True |
d | True | True | True | True |
e | True | True | True | True |
# 当值是dict时,我们可以分别传递值来检查每列
df.isin({'兵器': ['金箍棒', '九齿钉耙']})
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | False | False | True | False |
b | False | False | False | False |
c | False | False | True | False |
d | False | False | False | False |
e | False | False | False | False |
# 当值为Series或DataFrame时,索引和列必须匹配。
other = pd.DataFrame({'name': ['悟空', '-'], 'age': [1000, 33], '兵器': ['金箍棒', '-'], '力量': [100.0, 10.0]}, index=['a', 'b'])
df.isin(other)
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | True | True | True | True |
b | False | True | False | True |
c | False | False | False | False |
d | False | False | False | False |
e | False | False | False | False |
names = ['悟空', '八戒', '沙僧']
names = '|'.join(names)
names
'悟空|八戒|沙僧'
df['name'].str.contains(names, regex=True)
a True
b False
c True
d True
e False
Name: name, dtype: bool
df[df['name'].str.contains(names, regex=True)]
name | age | 兵器 | 力量 | |
---|---|---|---|---|
a | 悟空 | 1000 | 金箍棒 | 100.0 |
c | 八戒 | 800 | 九齿钉耙 | 60.0 |
d | 沙僧 | 700 | 降妖宝杖 | 50.0 |
pandas分组聚合|agg|transform|apply
缺省值判断 pd.isnull, pd.isna, pd.notna, pd.notnull, np.isnan, math.isnan 区别
pandas中DataFrame字典互转
pandas.concat实现DataFrame竖着拼接、横着拼接
pandas|找出某列最大值的所在的行
DataFrame——指定位置增加删除一行一列
AttributeError: module ‘pandas‘ has no attribute ‘isna‘
pandas–Series.str–字符串处理
list、ndarry、Series、DataFrame的创建、索引和选取
Series和DataFrame复合索引的创建和取值
pd.notnull
Pandas|DataFrame| 处理DataFrame中的inf值
由字典dictionary或列表list创建dataframe
pandas|DataFrame排序及分组排序
Pandas|DataFrame| DataFrame中的nan值处理
pandas|判断是否包含|contains|isin