以下内容,如何使用pandas提取含有指定字符串的行的方法进行解释说明。
行的提取(选择)方法
完全匹配
- ==
部分匹配
-
str.contains():包含一个特定的字符串
- 参数na:缺少值NaN处理
- 参数case:大小写我的处理
- 参数regex:使用正则表达式模式
- str.endswith():以特定字符串结尾
- str.startswith():以特定的字符串开头
- str.match():匹配正则表达式模式
要提取部分匹配的行,可以使用pandas的(str.xxx())方法,根据指定条件提取的字符串方法。
这次以以下数据为例
1
2
3
4
5
6
7
8
|
import pandas as pd df = pd.read_csv( './data/08/sample_pandas_normal.csv' ).head( 3 ) print (df) # name age state point # 0 Alice 24 NY 64 # 1 Bob 42 CA 92 # 2 Charlie 18 CA 70 |
行的提取(选择)方法
首先,展示如何从pandas.DataFrame中提取(选择)行以获得新的pandas.DataFrame。
使用布尔值的布尔列表(数组)或pandas.Series的话,只能提取(选择)True行。
1
2
3
4
5
6
|
mask = [ True , False , True ] df_mask = df[mask] print (df_mask) # name age state point # 0 Alice 24 NY 64 # 2 Charlie 18 CA 70 |
因此,对于具有字符串元素的列,是否能够获得根据条件的布尔列表就足够了。
完全匹配
==
如果元素与字符串完全匹配,则使用==获取为True的pandas.Series。
1
2
3
4
5
6
7
8
9
10
|
print (df[ 'state' ] = = 'CA' ) # 0 False # 1 True # 2 True # Name: state, dtype: bool print (df[df[ 'state' ] = = 'CA' ]) # name age state point # 1 Bob 42 CA 92 # 2 Charlie 18 CA 70 |
部分匹配
str.contains():包含一个特定的字符串
pandas.Series字符串方法str.contains()允许获取包含特定字符串的pandas.Series.
1
2
3
4
5
6
7
8
9
10
|
print (df[ 'name' ]. str .contains( 'li' )) # 0 True # 1 False # 2 True # Name: name, dtype: bool print (df[df[ 'name' ]. str .contains( 'li' )]) # name age state point # 0 Alice 24 NY 64 # 2 Charlie 18 CA 70 |
请注意,默认情况下,第一个参数中指定的字符串将作为正则表达式模式进行处理,如下所述。
参数na:缺少值NaN处理
如果元素是缺失值NaN,则默认情况下它将返回NaN而不是True或False。因此,使用pandas.Series提取该行是错误的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
df_nan = df.copy() df_nan.iloc[ 2 , 0 ] = float ( 'nan' ) print (df_nan) # name age state point # 0 Alice 24 NY 64 # 1 Bob 42 CA 92 # 2 NaN 18 CA 70 print (df_nan[ 'name' ]. str .contains( 'li' )) # 0 True # 1 False # 2 NaN # Name: name, dtype: object # print(df_nan[df_nan['name'].str.contains('li')]) # ValueError: cannot index with vector containing NA / NaN values |
可以通过str.contains()的参数na来指定替换NaN结果的值。
1
2
3
4
5
6
7
8
9
10
11
|
print (df_nan[ 'name' ]. str .contains( 'li' , na = False )) # 0 True # 1 False # 2 False # Name: name, dtype: bool print (df_nan[ 'name' ]. str .contains( 'li' , na = True )) # 0 True # 1 False # 2 True # Name: name, dtype: bool |
用作条件时,如果na = True,则选择NaN的行,如果na = False,则不选择NaN的行。
参数case:大小写我的处理
默认情况下,区分大小写。如果参数case为False,则case被忽略。
1
2
3
4
5
6
7
8
9
10
11
|
print (df[ 'name' ]. str .contains( 'LI' )) # 0 False # 1 False # 2 False # Name: name, dtype: bool print (df[ 'name' ]. str .contains( 'LI' , case = False )) # 0 True # 1 False # 2 True # Name: name, dtype: bool |
参数regex:使用正则表达式模式
使用str.contains()时要记住的一件事是,默认情况下,指定为第一个参数的字符串将作为正则表达式模式进行处理。
1
2
3
4
5
|
print (df[ 'name' ]. str .contains( 'i.*e' )) # 0 True # 1 False # 2 True # Name: name, dtype: bool |
如果参数ragex为False,则确定是否包含第一个参数的字符串本身。
1
2
3
4
5
|
print (df[ 'name' ]. str .contains( 'i.*e' , regex = False )) # 0 False # 1 False # 2 False # Name: name, dtype: bool |
例如,如果要判断是否包含正则表达式的特殊字符,例如?,。,*,则需要设置regex = False。当然,可以指定一个正则表达式模式,以转义\?等特殊字符。
请注意,默认值可能会导致错误。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
df_q = df.copy() df_q.iloc[ 2 , 0 ] + = '?' print (df_q) # name age state point # 0 Alice 24 NY 64 # 1 Bob 42 CA 92 # 2 Charlie? 18 CA 70 # print(df_q['name'].str.contains('?')) # error: nothing to repeat at position 0 print (df_q[ 'name' ]. str .contains( '?' , regex = False )) # 0 False # 1 False # 2 True # Name: name, dtype: bool print (df_q[ 'name' ]. str .contains( '\?' )) # 0 False # 1 False # 2 True # Name: name, dtype: bool |
str.contains()等同于re.search(),并且可以在flags参数中指定正则表达式标志。如稍后所述,还有对应于re.match()的str.match()。
请注意,下面要介绍的str.endswith()如果想要确定end ?,会更容易,如本例所示。
str.endswith():以特定字符串结尾
pandas.Series字符串方法str.endswith()可以获取以特定字符串结尾的pandas.Series。
1
2
3
4
5
6
7
8
9
10
|
print (df[ 'name' ]. str .endswith( 'e' )) # 0 True # 1 False # 2 True # Name: name, dtype: bool print (df[df[ 'name' ]. str .endswith( 'e' )]) # name age state point # 0 Alice 24 NY 64 # 2 Charlie 18 CA 70 |
str.endswith()也有一个参数na。如果要选择缺失值NaN的行,则设置na = True;如果不想选择,则将na = False设置。
没有参数case,因此它始终区分大小写。
另外,第一个参数的字符串在确定中照原样使用,而不作为正则表达式模式处理。
str.startswith():以特定的字符串开头
pandas.Series字符串方法str.startswith()可以获取以特定字符串开头的pandas.Series。
1
2
3
4
5
6
7
8
9
|
print (df[ 'name' ]. str .startswith( 'B' )) # 0 False # 1 True # 2 False # Name: name, dtype: bool print (df[df[ 'name' ]. str .startswith( 'B' )]) # name age state point # 1 Bob 42 CA 92 |
str.match():匹配正则表达式模式
pandas.Series字符串方法str.match()可以获取与正则表达式模式匹配的pandas.Series。
1
2
3
4
5
6
7
8
9
10
|
print (df[ 'name' ]. str .match( '.*i.*e' )) # 0 True # 1 False # 2 True # Name: name, dtype: bool print (df[df[ 'name' ]. str .match( '.*i.*e' )]) # name age state point # 0 Alice 24 NY 64 # 2 Charlie 18 CA 70 |
如上所述,str.match()对应于re.match(),并确定字符串的开头是否与模式匹配。如果不是一开始就为False。
1
2
3
4
5
6
7
8
9
10
11
|
print (df[ 'name' ]. str .match( '.*i' )) # 0 True # 1 False # 2 True # Name: name, dtype: bool print (df[ 'name' ]. str .match( 'i.*e' )) # 0 False # 1 False # 2 False # Name: name, dtype: bool |
当需要确定是否包括与模式匹配的部分时,不仅在开始时,而且默认使用与上述re.search()等效的re.contains()(regex = True)。
str.match()与str.contains()可以以相同的方式指定参数na,case和flag。
到此这篇关于Pandas提取含有指定字符串的行(完全匹配,部分匹配)的文章就介绍到这了,更多相关Pandas提取指定字符串的行内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_18351157/article/details/105362159