【5.1.2】Pandas检查列包含的字符串

一、方法

完全匹配

(df['col'].eq('exact_string')).any()

部分包含

df['col'].str.contains('partial_string').any()	

包含的次数

df['col'].str.contains('partial_string').sum()

二、案例

例1:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'C'],
                   'conference': ['East', 'East', 'South', 'West', 'West', 'East'],
                   'points': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

        team	conference   points
0	A	East         11
1	A	East	     8
2	A	South        10
3	B	West         6
4	B	West         6
5	C	East         5


#check if exact string 'Eas' exists in conference column
(df['conference'].eq('Eas')).any()

False


#check if partial string 'Eas' exists in conference column
df['conference'].str.contains('Eas').any()

True


#count occurrences of partial string 'Eas' in conference column
df['conference'].str.contains('East').sum()

3

参考资料

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn