pandas处理字符串1

来源:互联网 发布:商务部零售数据 编辑:程序博客网 时间:2024/05/22 17:40

pandas官网对序列字符串操作的说明
这篇博客主要是对一些基本的函数进行了一些实践和说明,如有不当,还希望大家指出来。

1.Series.str.capitalize()

将序列索引中的字符串或索引转成大写返回:转换序列后的对象或索引

示例:

>>> import pandas as pd>>> df = {'a':[1,2,3],'b':[4,5,6]}>>> df = pd.DataFrame(df)>>> print(df)   a  b0  1  41  2  52  3  6>>> df.index=['f','g','h']>>> df   a  bf  1  4g  2  5h  3  6>>> df.index.str.capitalize()Index(['F', 'G', 'H'], dtype='object')>>> df   a  bf  1  4g  2  5h  3  6

2.Series.str.cat(others=None, sep=None, na_rep=None)

用给定的字符链接序列或索引中的字符串返回:合并后的序列注:当na_rep为None,序列中的nan值将被忽略,如果指定,将用该字符代替

示例:

>>> df.index.str.cat(['1','2','3'],sep = ',')Index(['f,1', 'g,2', 'h,3'], dtype='object')>>> df.index.str.cat([['1','2','3'],['4','5','6']],sep='*')Index(['f*1*4', 'g*2*5', 'h*3*6'], dtype='object')>>> df.index.str.cat(sep='*')'f*g*h'>>> 

3.Series.str.center(width, fillchar=’ ‘)

用一个另外的字符对序列或者索引的左边和右边填充指定长度的字符width:填充后的总长度返回:填充后的序列或者索引对象

示例:

>>> df['hh'] = ['d','f','g']>>> df   a  b hhf  1  4  dg  2  5  fh  3  6  g>>> df['hh'].str.center(width = 3,fillchar='&')f    &d&g    &f&h    &g&Name: hh, dtype: object

4.Series.str.contains(pat, case=True, flags=0, na=nan, regex=True)

判断给定的字符串或者正则表达式是否在序列或者索引中返回:bool

示例:

>>> df   a  b hhf  1  4  dg  2  5  fh  3  6  g>>> df['hh'].str.contains('f')f    Falseg     Trueh    FalseName: hh, dtype: bool

5.Series.str.count(pat, flags=0, **kwargs)

计算pat在序列或者索引字符串中出现的次数

示例:

>>> df['count']=['hello','hello','hel']>>> df   a  b hh  countf  1  4  d  hellog  2  5  f  helloh  3  6  g    hel>>> df['count'].str.count('hel')f    1g    1h    1Name: count, dtype: int64>>> df['count'].str.count('l')f    2g    2h    1Name: count, dtype: int64

6.Series.str.decode(encoding, errors=’strict’)

对指定的编码方式进行解码(‘utf-8’,'gbk'等等)

7.Series.str.encode(encoding, errors=’strict’)

编码

8.Series.str.endswith(pat, na=nan)

判断是否已给定的pat结尾

示例:

>>> df['count'].str.endswith('lo')f     Trueg     Trueh    FalseName: count, dtype: bool

9.Series.str.extract(pat, flags=0, expand=None)

对给定的正则表达式进行提取expand : bool, default False    If True, return DataFrame.    If False, return Series/Index/DataFrame.
示例:>>> s = Series(['a1', 'b2', 'c3'])>>> s.str.extract('([ab])(\d)')     0    10    a    11    b    22  NaN  NaN>>> s.str.extract('([ab])?(\d)')     0  10    a  11    b  22  NaN  3>>> s.str.extract('[ab](\d)', expand=True)     00    11    22  NaN>>> s.str.extract('[ab](\d)', expand=False)0      11      22    NaNdtype: object

10.Series.str.extractall(pat, flags=0)

对于本序列中的每一个字符串,从正则表达式的所有匹配中提取组。
示例:>>> s= pd.Series(["a1a2", "b1", "c1"], index=["A", "B", "C"])>>> sA    a1a2B      b1C      c1dtype: object>>> s.str.extractall('[ab](\d)')         0  match   A 0      1  1      2B 0      1>>> s.str.extractall("[ab](?P<digit>\d)")        digit  match      A 0         1  1         2B 0         1>>> s.str.extractall("(?P<letter>[ab])(?P<digit>\d)")        letter digit  match             A 0          a     1  1          a     2B 0          b     1>>> s.str.extractall("(?P<letter>[ab])?(?P<digit>\d)")        letter digit  match             A 0          a     1  1          a     2B 0          b     1C 0        NaN     1