6.19. DataFrame Extract
Series.str.split(regex, expand=True)- Split strings around given separator/delimiterSeries.str.extract(regex, expand=True)- Extract capture groups in the regex pat as columns in a DataFrameSeries.str.extractall()- Extract capture groups in the regex pat as columns in DataFrameSeries.str.findall(regex)- Find all occurrences of pattern or regular expression in the Series/IndexSeries.str.fullmatch(regex)- Determine if each string entirely matches a regular expressionSeries.dt.strftime(...)- formatted strings specified by date_format, which supports the same string format as the python standard librarySeries.dt.date- Returns numpy array of python datetime.date objectsSeries.dt.time- Returns numpy array of datetime.time objectsSeries.dt.timez- Returns numpy array of datetime.time objects with timezone information
6.19.1. SetUp
>>> import pandas as pd
>>>
>>> df = pd.DataFrame([
... {'firstname': 'Alice', 'lastname': 'Apricot', 'email': 'alice@example.com', 'lastlogin': pd.Timestamp('2000-01-01'), 'groups': 'users;staff'},
... {'firstname': 'Bob', 'lastname': 'Blackthorn', 'email': 'bob@example.com', 'lastlogin': pd.Timestamp('2000-01-02'), 'groups': 'users;staff'},
... {'firstname': 'Carol', 'lastname': 'Corn', 'email': 'carol@example.com', 'lastlogin': pd.Timestamp('2000-01-03'), 'groups': 'users'},
... {'firstname': 'Dave', 'lastname': 'Durian', 'email': 'dave@example.org', 'lastlogin': pd.Timestamp('2000-01-04'), 'groups': 'users'},
... {'firstname': 'Eve', 'lastname': 'Elderberry', 'email': 'eve@example.org', 'lastlogin': pd.Timestamp('2000-01-05'), 'groups': 'users;staff;admins'},
... {'firstname': 'Mallory', 'lastname': 'Melon', 'email': 'mallory@example.net', 'lastlogin': pd.NaT, 'groups': None}
... ])
>>> df
firstname lastname email lastlogin groups
0 Alice Apricot alice@example.com 2000-01-01 users;staff
1 Bob Blackthorn bob@example.com 2000-01-02 users;staff
2 Carol Corn carol@example.com 2000-01-03 users
3 Dave Durian dave@example.org 2000-01-04 users
4 Eve Elderberry eve@example.org 2000-01-05 users;staff;admins
5 Mallory Melon mallory@example.net NaT NaN
6.19.2. Split
>>> df['email'].str.split('@')
0 [alice, example.com]
1 [bob, example.com]
2 [carol, example.com]
3 [dave, example.org]
4 [eve, example.org]
5 [mallory, example.net]
Name: email, dtype: object
>>> df['email'].str.split('@', expand=True)
0 1
0 alice example.com
1 bob example.com
2 carol example.com
3 dave example.org
4 eve example.org
5 mallory example.net
>>> new = pd.DataFrame()
>>> new[['username','domain']] = df['email'].str.split('@', expand=True)
>>>
>>> new
username domain
0 alice example.com
1 bob example.com
2 carol example.com
3 dave example.org
4 eve example.org
5 mallory example.net
6.19.3. Extract
>>> df
firstname lastname email lastlogin groups
0 Alice Apricot alice@example.com 2000-01-01 users;staff
1 Bob Blackthorn bob@example.com 2000-01-02 users;staff
2 Carol Corn carol@example.com 2000-01-03 users
3 Dave Durian dave@example.org 2000-01-04 users
4 Eve Elderberry eve@example.org 2000-01-05 users;staff;admins
5 Mallory Melon mallory@example.net NaT NaN
>>>
>>> df['email'].str.extract(r'([a-z]+)@example.*')
0
0 alice
1 bob
2 carol
3 dave
4 eve
5 mallory
6.19.4. Findall
>>> df['groups'].str.findall('[a-z]+')
0 [users, staff]
1 [users, staff]
2 [users]
3 [users]
4 [users, staff, admins]
5 NaN
Name: groups, dtype: object