除了上述介绍的五类字符串操作有关的函数之外, str
对象上还定义了一些实用的其他方法,在此进行介绍:
1. 字母型函数
upper, lower, title, capitalize, swapcase
这五个函数主要用于字母的大小写转化,从下面的例子中就容易领会其功能:
In [86]: s = pd.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
In [87]: s.str.upper()
Out[87]:
0 LOWER
1 CAPITALS
2 THIS IS A SENTENCE
3 SWAPCASE
dtype: object
In [88]: s.str.lower()
Out[88]:
0 lower
1 capitals
2 this is a sentence
3 swapcase
dtype: object
In [89]: s.str.title()
Out[89]:
0 Lower
1 Capitals
2 This Is A Sentence
3 Swapcase
dtype: object
In [90]: s.str.capitalize()
Out[90]:
0 Lower
1 Capitals
2 This is a sentence
3 Swapcase
dtype: object
In [91]: s.str.swapcase()
Out[91]:
0 LOWER
1 capitals
2 THIS IS A SENTENCE
3 sWaPcAsE
dtype: object
2. 数值型函数
这里着重需要介绍的是 pd.to_numeric
方法,它虽然不是 str
对象上的方法,但是能够对字符格式的数值进行快速转换和筛选。其主要参数包括 errors
和 downcast
分别代表了非数值的处理模式和转换类型。其中,对于不能转换为数值的有三种 errors
选项, raise, coerce, ignore
分别表示直接报错、设为缺失以及保持原来的字符串。
In [92]: s = pd.Series(['1', '2.2', '2e', '??', '-2.1', '0'])
In [93]: pd.to_numeric(s, errors='ignore')
Out[93]:
0 1
1 2.2
2 2e
3 ??
4 -2.1
5 0
dtype: object
In [94]: pd.to_numeric(s, errors='coerce')
Out[94]:
0 1.0
1 2.2
2 NaN
3 NaN
4 -2.1
5 0.0
dtype: float64
在数据清洗时,可以利用 coerce
的设定,快速查看非数值型的行:
In [95]: s[pd.to_numeric(s, errors='coerce').isna()]
Out[95]:
2 2e
3 ??
dtype: object
3. 统计型函数
count
和 len
的作用分别是返回出现正则模式的次数和字符串的长度:
In [96]: s = pd.Series(['cat rat fat at', 'get feed sheet heat'])
In [97]: s.str.count('[r|f]at|ee')
Out[97]:
0 2
1 2
dtype: int64
In [98]: s.str.len()
Out[98]:
0 14
1 19
dtype: int64
4. 格式型函数
格式型函数主要分为两类,第一种是除空型,第二种是填充型。其中,第一类函数一共有三种,它们分别是 strip, rstrip, lstrip
,分别代表去除两侧空格、右侧空格和左侧空格。这些函数在数据清洗时是有用的,特别是列名含有非法空格的时候。
In [99]: my_index = pd.Index([' col1', 'col2 ', ' col3 '])
In [100]: my_index.str.strip().str.len()
Out[100]: Int64Index([4, 4, 4], dtype='int64')
In [101]: my_index.str.rstrip().str.len()
Out[101]: Int64Index([5, 4, 5], dtype='int64')
In [102]: my_index.str.lstrip().str.len()
Out[102]: Int64Index([4, 5, 5], dtype='int64')
对于填充型函数而言, pad
是最灵活的,它可以选定字符串长度、填充的方向和填充内容:
In [103]: s = pd.Series(['a','b','c'])
In [104]: s.str.pad(5,'left','*')
Out[104]:
0 ****a
1 ****b
2 ****c
dtype: object
In [105]: s.str.pad(5,'right','*')
Out[105]:
0 a****
1 b****
2 c****
dtype: object
In [106]: s.str.pad(5,'both','*')
Out[106]:
0 **a**
1 **b**
2 **c**
dtype: object
上述的三种情况可以分别用 rjust, ljust, center
来等效完成,需要注意 ljust
是指右侧填充而不是左侧填充:
In [107]: s.str.rjust(5, '*')
Out[107]:
0 ****a
1 ****b
2 ****c
dtype: object
In [108]: s.str.ljust(5, '*')
Out[108]:
0 a****
1 b****
2 c****
dtype: object
In [109]: s.str.center(5, '*')
Out[109]:
0 **a**
1 **b**
2 **c**
dtype: object
在读取 excel
文件时,经常会出现数字前补0的需求,例如证券代码读入的时候会把”000007”作为数值7来处理, pandas
中除了可以使用上面的左侧填充函数进行操作之外,还可用 zfill
来实现。
In [110]: s = pd.Series([7, 155, 303000]).astype('string')
In [111]: s.str.pad(6,'left','0')
Out[111]:
0 000007
1 000155
2 303000
dtype: string
In [112]: s.str.rjust(6,'0')
Out[112]:
0 000007
1 000155
2 303000
dtype: string
In [113]: s.str.zfill(6)
Out[113]:
0 000007
1 000155
2 303000
dtype: string