Pandas 係列'str.extractall(~)
提取物全部使用正則表達式匹配的子字符串。
注意
要提取第一個匹配項而不是所有匹配項,請使用 str.extract(~)
。
參數
1. pat
| str
要匹配的正則表達式。
2. flags
| int
| optional
從 re
庫設置的標誌(例如 re.IGNORECASE
)。可以通過將多個標誌與按位 |
組合來設置它們(例如 re.IGNORECASE | re.MULTILINE
)。
返回值
多索引 DataFrame。
例子
基本用法
考慮以下 DataFrame :
import pandas as pd
df = pd.DataFrame({'A':['k23','45k','67k89']}, index=['a','b','c'])
df
A
a k23
b 45k
c 67k89
要獲取與給定正則表達式匹配的提取子字符串:
df['A'].str.extractall('(\d+)') # returns a multi-index DataFrame
0
match
a 0 23
b 0 45
c 0 67
1 89
這裏,輸入字符串是一個正則表達式,\d+
表示一個數字,而()
表示我們要提取的部分。
由於生成的 DataFrame 是一個多索引,因此我們可以獲得特定索引的匹配,如下所示:
df_result = df['A'].str.extractall('(\d+)')
df_result.loc['c']
0
match
0 67
1 89
多個捕獲組
考慮以下 DataFrame :
import pandas as pd
df = pd.DataFrame({'A':['k23','45y','67k89']}, index=['a','b','c'])
df
A
a k23
b 45y
c 67k89
我們可以使用多個括號捕獲多個組:
df['A'].str.extractall('(\d+)([ky])')
0 1
match
b 0 45 y
c 0 67 k
相關用法
- Python Pandas Series str extract方法用法及代碼示例
- Python Pandas Series str split方法用法及代碼示例
- Python Pandas Series str center方法用法及代碼示例
- Python Pandas Series str pad方法用法及代碼示例
- Python Pandas Series str replace方法用法及代碼示例
- Python Pandas Series str len方法用法及代碼示例
- Python Pandas Series str lower方法用法及代碼示例
- Python Pandas Series str strip方法用法及代碼示例
- Python Pandas Series str rstrip方法用法及代碼示例
- Python Pandas Series str lstrip方法用法及代碼示例
- Python Pandas Series string contains方法用法及代碼示例
- Python Pandas Series to_list方法用法及代碼示例
- Python Pandas Series between方法用法及代碼示例
- Python Pandas Series map方法用法及代碼示例
- Python Pandas Series hasnans屬性用法及代碼示例
- Python Pandas Series is_monotonic屬性用法及代碼示例
- Python Pandas Series to_frame方法用法及代碼示例
- Python Pandas Series zfill方法用法及代碼示例
- Python Pandas Series argmax方法用法及代碼示例
- Python Pandas Series is_monotonic_increasing屬性用法及代碼示例
- Python Pandas Series is_unique屬性用法及代碼示例
- Python Pandas Series argmin方法用法及代碼示例
- Python Pandas Series value_counts方法用法及代碼示例
- Python Pandas Series is_monotonic_decreasing屬性用法及代碼示例
- Python Pandas Series.cumsum()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas Series str | extractall method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。