数据清洗和准备2

#字符串操作
#字符串对象方法
#split
val = 'a,b, guido'
val.split(',')
['a', 'b', ' guido']
#strip
pieces = [x.strip()for x in val.split(',')]#去除空白符
pieces
['a', 'b', 'guido']
first,second,third = pieces
first + '::' + second + '::' + third
'a::b::guido'
'::'.join(pieces)
'a::b::guido'
'guido' in val
True
val.index(',')
1
val.find(':')
-1
val.count(',')
2
val.replace(',','::')
'a::b:: guido'
val.replace(',','')
'ab guido'
#正则表达式
import re
text = "foo    bar\t baz  \tqux"
re.split('\s+',text)
['foo', 'bar', 'baz', 'qux']
regex = re.compile('\s+')
regex.split(text)
['foo', 'bar', 'baz', 'qux']
regex.findall(text)
['    ', '\t ', '  \t']
text = """Dave [email protected]
Steve [email protected]
Rob [email protected]
Ryan [email protected]
"""
pattern =  r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{3}'
regex = re.compile(pattern,flags = re.IGNORECASE)
regex.findall(text)
['[email protected]', '[email protected]', '[email protected]', '[email protected]']
m = regex.search(text)
m

text[m.start():m.end()]
'[email protected]'
print(regex.match(text)) #从头开始匹配
None
print(regex.sub('REDACTED',text))   #替换
Dave REDACTED
Steve REDACTED
Rob REDACTED
Ryan REDACTED
pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'
regex = re.compile(pattern,flags=re.IGNORECASE)
m = regex.match('[email protected]')
m.groups()
('wesm', 'bright', 'net')
regex.findall(text)
[('dave', 'google', 'com'),
 ('steve', 'gmail', 'com'),
 ('rob', 'gmail', 'com'),
 ('ryan', 'yahoo', 'com')]
print(regex.sub(r'Username:\1,Domain:\2,Suffix:\3',text))#符号\1对应第一个匹配的组
Dave Username:dave,Domain:google,Suffix:com
Steve Username:steve,Domain:gmail,Suffix:com
Rob Username:rob,Domain:gmail,Suffix:com
Ryan Username:ryan,Domain:yahoo,Suffix:com
#pandas的矢量化字符串函数
import pandas as pd
import numpy as np
data =  {'Dave': '[email protected]', 'Steve': '[email protected]',
         'Rob': '[email protected]', 'Wes': np.nan}
data = pd.Series(data)
data
Dave     [email protected]
Steve    [email protected]
Rob        [email protected]
Wes                  NaN
dtype: object
data.isnull()
Dave     False
Steve    False
Rob      False
Wes       True
dtype: bool
data.str.contains('gmail')
Dave     False
Steve     True
Rob       True
Wes        NaN
dtype: object
pattern
'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})'
data.str.findall(pattern,flags=re.IGNORECASE)
Dave     [(dave, google, com)]
Steve    [(steve, gmail, com)]
Rob        [(rob, gmail, com)]
Wes                        NaN
dtype: object
matches = data.str.match(pattern,flags = re.IGNORECASE)
matches
Dave     True
Steve    True
Rob      True
Wes       NaN
dtype: object
matches.str.get(1)
Dave    NaN
Steve   NaN
Rob     NaN
Wes     NaN
dtype: float64
data.str[:5]
Dave     dave@
Steve    steve
Rob      rob@g
Wes        NaN
dtype: object

你可能感兴趣的:(利用python进行数据分析)