文字列操作
文字列オブジェクトのメソッド
code: Python
val = 'a,b, guido'
val.split(',')
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
# stripは前後の空白と改行を取り除く
pieces
--------------------------------------------------------------------------
--------------------------------------------------------------------------
code: Python
'::'.join(pieces)
--------------------------------------------------------------------------
'a::b::guido'
--------------------------------------------------------------------------
code: Python
val.count(',')
--------------------------------------------------------------------------
2
--------------------------------------------------------------------------
code: Python
val.replace(',', '::')
--------------------------------------------------------------------------
'a::b:: guido'
--------------------------------------------------------------------------
pandasにおける文字列関数のベクトル化
code: Python
data = {'Dave': 'dave@google.com', 'Steve': 'steve@gmail.com',
'Rob': 'rob@gmail.com', 'Wes': np.nan}
data = pd.Series(data)
data
--------------------------------------------------------------------------
Dave dave@google.com
Rob rob@gmail.com
Steve steve@gmail.com
Wes NaN
dtype: object
--------------------------------------------------------------------------
code: Python
data.str.contains('gmail')
--------------------------------------------------------------------------
Dave False
Rob True
Steve True
Wes NaN
dtype: object
--------------------------------------------------------------------------
code: Python
import re
data.str.findall(pattern, flags=re.IGNORECASE)
--------------------------------------------------------------------------
Wes NaN
dtype: object
--------------------------------------------------------------------------
code: Python
matches = data.str.match(pattern, flags=re.IGNORECASE)
matches
--------------------------------------------------------------------------
Dave True
Rob True
Steve True
Wes NaN
dtype: object
--------------------------------------------------------------------------