您的位置:首页 > 编程语言 > Python开发

利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(5) .

2014-07-03 11:28 1311 查看
来自:http://blog.csdn.net/ssw_1990/article/details/26584555

1、字符串操作

Python能够成为流行的数据处理语言,部分原因是其简单易用的字符串和文本处理功能。大部分文本运算都直接做成了字符串对象的内置方法。对于更为复杂的模式匹配和文本操作,则可能需要用到正则表达式。pandas对此进行了加强,它使你能够对数组数据应用字符串表达式和正则表达式,而且能处理烦人的缺失数据。

2、字符串对象方法

对于大部分字符串处理应用而言,内置的字符串方法已经能够满足要求了。例如,以逗号分隔的字符串可以用split拆分成数段:

[python]
view plaincopyprint?





In [81]: val = 'a,b, guido'  
   
In [82]: val.split(',')  
Out[82]: ['a', 'b', ' guido']  

In [81]: val = 'a,b, guido'

In [82]: val.split(',')
Out[82]: ['a', 'b', ' guido']

split常常结合strip(用于修剪空白符(包括换行符))一起使用:

[python]
view plaincopyprint?





In [83]: pieces = [x.strip() for x in val.split(',')]  
  
In [84]: pieces  
Out[84]: ['a', 'b', 'guido']  

In [83]: pieces = [x.strip() for x in val.split(',')]

In [84]: pieces
Out[84]: ['a', 'b', 'guido']

利用加法,可以将这些子字符串以双冒号分隔符的形式连接起来:

[python]
view plaincopyprint?





In [88]: first, second, third = pieces  
  
In [89]: first + '::' + second + '::' + third  
Out[89]: 'a::b::guido'  

In [88]: first, second, third = pieces

In [89]: first + '::' + second + '::' + third
Out[89]: 'a::b::guido'

但这种方式并不是很实用。一种更快更符合Python风格的方式是,向字符串“::”的join方法传入一个列表或元组:

[python]
view plaincopyprint?





In [90]: '::'.join(pieces)  
Out[90]: 'a::b::guido'  

In [90]: '::'.join(pieces)
Out[90]: 'a::b::guido'

另一类方法关注的是子串定位。检测子串的最佳方式是利用Python的in关键字(当然还可以使用index和find):

[python]
view plaincopyprint?





In [92]: 'guido' in val  
Out[92]: True  
  
In [93]: val.index(',')  
Out[93]: 1  
  
In [95]: val.find(':')  
Out[95]: -1  

In [92]: 'guido' in val
Out[92]: True

In [93]: val.index(',')
Out[93]: 1

In [95]: val.find(':')
Out[95]: -1

注意find和index的区别:如果找不到字符串,index将会引发一个异常(而不是返回-1):

[python]
view plaincopyprint?





In [96]: val.index(':')  
---------------------------------------------------------------------------  
ValueError                                Traceback (most recent call last)  
/home/wss/program/python/<ipython-input-96-280f8b2856ce> in <module>()  
----> 1 val.index(':')  
  
ValueError: substring not found  

In [96]: val.index(':')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/wss/program/python/<ipython-input-96-280f8b2856ce> in <module>()
----> 1 val.index(':')

ValueError: substring not found

此外还有一个count函数,它可以返回指定子串的出现次数:

[python]
view plaincopyprint?





In [97]: val.count(',')  
Out[97]: 2  

In [97]: val.count(',')
Out[97]: 2

replace用于将指定模式替换为另一个模式。它也常常用于删除模式:传入空字符串。

[python]
view plaincopyprint?





In [98]: val.replace(',', '::')  
Out[98]: 'a::b:: guido'  
  
In [99]: val.replace(',', '')  
Out[99]: 'ab guido'  

In [98]: val.replace(',', '::')
Out[98]: 'a::b:: guido'

In [99]: val.replace(',', '')
Out[99]: 'ab guido'

说明:

这些运算大部分都能使用正则表达式实现。



3、正则表达式

正则表达式(regex)提供了一种灵活的在文本中搜索或匹配字符串模式的方式。正则表达式是根据正则表达式语言编写的字符串。Python内置的re模块负责对字符串应用正则表达式。

推荐书籍:《Learn Regex The Hard Way》

re模块的函数可以分为三个大类:模式匹配、替换以及拆分。当然,它们之间是相辅相成的。一个regex描述了需要在文本中定位的一个模式,它可以用于许多目的。我们先来看一个简单的例子:假设我想要拆分一个字符串,分隔符为数量不定的一组空白符(制表符、空格、换行符等)。描述一个或多个空白符的regex是\s+:

[python]
view plaincopyprint?





In [100]: import re  
  
In [101]: text = "foo bar\t baz \tqux"  
  
In [102]: re.split('\s+', text)  
Out[102]: ['foo', 'bar', 'baz', 'qux']  

In [100]: import re

In [101]: text = "foo bar\t baz \tqux"

In [102]: re.split('\s+', text)
Out[102]: ['foo', 'bar', 'baz', 'qux']

调用re.split('\s+', text)时,正则表达式会先被编译,然后再在text上调用其split方法。你可以用re.compile自己编译regex以得到一个可重用的regex对象:

[python]
view plaincopyprint?





In [103]: regex = re.compile('\s+')  
  
In [104]: regex.split(text)  
Out[104]: ['foo', 'bar', 'baz', 'qux']  

In [103]: regex = re.compile('\s+')

In [104]: regex.split(text)
Out[104]: ['foo', 'bar', 'baz', 'qux']

如果只希望得到匹配regex的所有模式,则可以使用findall方法:

[python]
view plaincopyprint?





In [105]: regex.findall(text)  
Out[105]: [' ', '\t ', ' \t']  

In [105]: regex.findall(text)
Out[105]: [' ', '\t ', ' \t']

注意:

如果想避免正则表达式中不需要的转义(\),则可以使用原始字符串字面量如r'C:\x'(也可以编写其等价式'C:\\x')。

如果打算对许多字符串应用同一条正则表达式,强烈建议通过re.compile创建regex对象。这样将可以节省大量的CPU时间。

match和search跟findall功能类似。findall返回的是字符串中所有的匹配项,而search则只返回第一个匹配项。match更加严格,它只匹配字符串的首部。来看一个小例子,假设我们有一段文本以及一条能够识别大部分电子邮件地址的正则表达式:

[python]
view plaincopyprint?





In [106]: text = """Dave dave@google.com 
   .....:           Steve steve@gmail.com 
   .....:           Rob rob@gmail.com 
   .....:           Ryan ryan@yahoo.com 
   .....:        """  
  
In [107]: pattern = r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'  
  
In [108]: # re.IGNORECASE的作用是使用正则表达式对大小写不敏感
  
  
In [109]: regex = re.compile(pattern, flags=re.IGNORECASE)  

In [106]: text = """Dave dave@google.com
.....:           Steve steve@gmail.com
.....:           Rob rob@gmail.com
.....:           Ryan ryan@yahoo.com
.....:        """

In [107]: pattern = r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'

In [108]: # re.IGNORECASE的作用是使用正则表达式对大小写不敏感

In [109]: regex = re.compile(pattern, flags=re.IGNORECASE)

对text使用findall将得到一组电子邮件地址:

[python]
view plaincopyprint?





In [116]: regex.findall(text)  
Out[116]: ['dave@google.com', 'steve@gmail.com', 'rob@gmail.com', 'ryan@yahoo.com']  

In [116]: regex.findall(text)
Out[116]: ['dave@google.com', 'steve@gmail.com', 'rob@gmail.com', 'ryan@yahoo.com']

search返回的是文本中第一个电子邮件地址(以特殊的匹配项对象形式返回)。对于上面那个regex,匹配项对象只能告诉我们模式在原字符串中的起始和结束位置:

[python]
view plaincopyprint?





In [117]: m = regex.search(text)  
  
In [118]: m  
Out[118]: <_sre.SRE_Match at 0x9dc81e0>  
  
In [119]: text[m.start():m.end()]  
Out[119]: 'dave@google.com'  

In [117]: m = regex.search(text)

In [118]: m
Out[118]: <_sre.SRE_Match at 0x9dc81e0>

In [119]: text[m.start():m.end()]
Out[119]: 'dave@google.com'

regex.match则将返回None,因为它只匹配出现在字符串开头的模式:

[python]
view plaincopyprint?





In [120]: print regex.match(text)  
None  

In [120]: print regex.match(text)
None

另外还有一个sub方法,它会将匹配到的模式替换为指定字符串,并返回所得到的新字符串:

[python]
view plaincopyprint?





In [121]: print regex.sub('REDACTED', text)  
Dave REDACTED  
Steve REDACTED  
Rob REDACTED  
Ryan REDACTED  

In [121]: print regex.sub('REDACTED', text)
Dave REDACTED
Steve REDACTED
Rob REDACTED
Ryan REDACTED

假设你不仅想要找出电子邮件地址,还想将各个地址分成3个部分:用户名、域名以及域后缀。要实现此功能,只需将待分段的模式的各部分用圆括号包起来即可:

[python]
view plaincopyprint?





In [122]: pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'  
  
In [123]: regex = re.compile(pattern, flags=re.IGNORECASE)  

In [122]: pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'

In [123]: regex = re.compile(pattern, flags=re.IGNORECASE)

由这种正则表达式所产生的匹配项对象,可以通过其groups方法返回一个由模式各段组成的元组:

[python]
view plaincopyprint?





In [124]: m = regex.match('wesm@bright.net')  
  
In [125]: m.groups()  
Out[125]: ('wesm', 'bright', 'net')  

In [124]: m = regex.match('wesm@bright.net')

In [125]: m.groups()
Out[125]: ('wesm', 'bright', 'net')

对于带有分组功能的模块,findall会返回一个元组列表:

[python]
view plaincopyprint?





In [126]: regex.findall(text)  
Out[126]:  
[('dave', 'google', 'com'),  
('steve', 'gmail', 'com'),  
('rob', 'gmail', 'com'),  
('ryan', 'yahoo', 'com')]  

In [126]: regex.findall(text)
Out[126]:
[('dave', 'google', 'com'),
('steve', 'gmail', 'com'),
('rob', 'gmail', 'com'),
('ryan', 'yahoo', 'com')]

sub还能通过诸如\1、\2之类的特殊符号访问各匹配项中的分组:

[python]
view plaincopyprint?





In [127]: print regex.sub(r'Username: \1, Domain: \2, Suffix: \3', text)  
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  

In [127]: print regex.sub(r'Username: \1, Domain: \2, Suffix: \3', text)
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

对上面那个电子邮件正则表达式做一点小变动:为各个匹配分组加上一个名称。如下所示:

[python]
view plaincopyprint?





regex = re.compile(r""" 
(?P<username>[A-Z0-9._%+-]+) 

(?P<domain>[A-Z0-9.-]+) 
\. 
(?P<suffix>[A-Z]{2,4})""", flags=re.IGNORECASE|re.VERBOSE)  

regex = re.compile(r"""
(?P<username>[A-Z0-9._%+-]+)
@
(?P<domain>[A-Z0-9.-]+)
\.
(?P<suffix>[A-Z]{2,4})""", flags=re.IGNORECASE|re.VERBOSE)

由这种正则表达式所产生的匹配项对象可以得到一个简单易用的带有分组名称的字典:

[python]
view plaincopyprint?





In [128]: m = regex.match('wesm@bright.net')  
  
In [129]: m.groupdict()  
Out[129]: {'domain': 'bright', 'suffix': 'net', 'username': 'wesm'}  

In [128]: m = regex.match('wesm@bright.net')

In [129]: m.groupdict()
Out[129]: {'domain': 'bright', 'suffix': 'net', 'username': 'wesm'}




4、pandas中矢量化的字符串函数

清理待分析的散乱数据时,常常需要做一些字符串规整化工作。更为复杂的情况是,含有字符串的列有时还含有缺失数据:

[python]
view plaincopyprint?





In [1]: data = {'Dave': 'dave@google.com', 'Steve': 'steve@gmail.com',  
   ...:         'Rob': 'rob@gmail.com', 'Wes': np.nan}  
  
In [2]: data = pd.Series(data)  
  
In [3]: data   
Out[3]:   
Dave   dave@google.com   
Rob      rob@gmail.com   
Steve  steve@gmail.com   
Wes                NaN   
  
In [4]: data.isnull()  
Out[4]:  
Dave   False  
Rob    False  
Steve  False  
Wes     True  

In [1]: data = {'Dave': 'dave@google.com', 'Steve': 'steve@gmail.com',
...:         'Rob': 'rob@gmail.com', 'Wes': np.nan}

In [2]: data = pd.Series(data)

In [3]: data
Out[3]:
Dave   dave@google.com
Rob      rob@gmail.com
Steve  steve@gmail.com
Wes                NaN

In [4]: data.isnull()
Out[4]:
Dave   False
Rob    False
Steve  False
Wes     True

通过data.map,所有字符串和正则表达式方法都能被应用于(传入lambda表达式或其他函数)各个值,但是如果存在NA就会报错。为了解决这个问题,Series有一些能够跳过NA值的字符串操作方法。通过Series的str属性即可访问这些方法。例如,我们可以通过str.contains检查各个电子邮件地址是否含有“gmail”:

[python]
view plaincopyprint?





In [5]: data.str.contains('gmail')  
Out[5]:   
Dave   False  
Rob     True  
Steve   True  
Wes      NaN  

In [5]: data.str.contains('gmail')
Out[5]: 
Dave   False
Rob     True
Steve   True
Wes      NaN

这里也可以使用正则表达式,还可以加上任意re选项(如IGNORECASE):

[python]
view plaincopyprint?





In [6]: pattern  
Out[6]: '([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})'  
  
In [7]: data.str.findall(pattern, flags=re.IGNORECASE)  
Out[7]:   
Dave   [('dave', 'google', 'com')]  
Rob    [('rob', 'gmail', 'com')]  
Steve  [('steve', 'gmail', 'com')]  
Wes    NaN  

In [6]: pattern
Out[6]: '([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})'

In [7]: data.str.findall(pattern, flags=re.IGNORECASE)
Out[7]: 
Dave   [('dave', 'google', 'com')]
Rob    [('rob', 'gmail', 'com')]
Steve  [('steve', 'gmail', 'com')]
Wes    NaN

有两个办法可以实现矢量化的元素获取操作:要么使用str.get,要么在str属性上使用索引。

[python]
view plaincopyprint?





In [8]: matches = data.str.match(pattern, flags=re.IGNORECASE)  
  
In [9]: matches  
Out[9]:  
Dave  ('dave', 'google', 'com')  
Rob     ('rob', 'gmail', 'com')  
Steve ('steve', 'gmail', 'com')  
Wes                         NaN  
  
In [10]: matches.str.get(1)   
Out[10]:   
Dave    google   
Rob      gmail   
Steve    gmail   
Wes        NaN   
  
In [11]: matches.str[0]  
Out[11]:  
Dave    dave  
Rob      rob  
Steve  steve  
Wes      NaN  

In [8]: matches = data.str.match(pattern, flags=re.IGNORECASE)

In [9]: matches
Out[9]:
Dave  ('dave', 'google', 'com')
Rob     ('rob', 'gmail', 'com')
Steve ('steve', 'gmail', 'com')
Wes                         NaN

In [10]: matches.str.get(1)
Out[10]:
Dave    google
Rob      gmail
Steve    gmail
Wes        NaN

In [11]: matches.str[0]
Out[11]:
Dave    dave
Rob      rob
Steve  steve
Wes      NaN

你可以利用下面这种代码对字符串进行子串截取:

[python]
view plaincopyprint?





In [12]: data.str[:5]  
Out[12]:  
Dave    dave@  
Rob     rob@g  
Steve   steve  
Wes       NaN  

In [12]: data.str[:5]
Out[12]:
Dave    dave@
Rob     rob@g
Steve   steve
Wes       NaN


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐