Try it here
Subscribe
Python series

Pandas Series.str.extract()

pandas_series.str.extract()

Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.extract() function is used to extract capture groups in the regex pat as columns in a DataFrame. For each subject string in the Series, extract groups from the first match of regular expression pat.

Parameters
patstr

Regular expression pattern with capturing groups.

flagsint, default 0 (no flags)

Flags from the re module, e.g. re.IGNORECASE, that modify regular expression matching for things like case, spaces, etc. For more details, see re.

expandbool, default True

If True, return DataFrame with one column per capture group. If False, return a Series/Index if there is one capture group or DataFrame if there are multiple capture groups.

Returns
DataFrame or Series or Index

A DataFrame with one row for each subject string, and one column for each group. Any capture group names in regular expression pat will be used for column names; otherwise capture group numbers will be used. The dtype of each result column is always object, even when no match is found. If expand=False and pat has only one capture group, then return a Series (if subject is a Series) or Index (if subject is an Index).

Example:

A pattern with two groups will return a DataFrame with two columns. Non-matches will be NaN.

>>> s=pd.Series(['name1','name2','name3'])
>>> s.str.extract('([a-z]*(\d))')
       0  1
0  name1  1
1  name2  2
2  name3  3
>>> 

Writer profile pic

Steve on Jun 22, 2020 at 05:04 am


This article is contributed by Steve. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.