加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

pandas数据框的str列内置的方法详解

(2019-01-07 11:43:23)
标签:

python

pandas

str

function

分类: Python

在使用pandas框架的DataFrame的过程中,如果需要处理一些字符串的特性,例如判断某列是否包含一些关键字,某列的字符长度是否小于3等等这种需求,如果掌握str列内置的方法,处理起来会方便很多。

        下面我们来详细了解一下,Series类的str自带的方法有哪些。

1、cat() 拼接字符串
        例子:
        >>> Series(['a', 'b', 'c']).str.cat(['A', 'B', 'C'], sep=',')
        a,A
        b,B
        c,C
        dtype: object
        >>> Series(['a', 'b', 'c']).str.cat(sep=',')
        'a,b,c'
        >>> Series(['a', 'b']).str.cat([['x', 'y'], ['1', '2']], sep=',')
           a,x,1
           b,y,2
        dtype: object
2、split() 切分字符串
        >>> import numpy,pandas;
        >>> pandas.Series(['a_b_c', 'c_d_e', numpy.nan, 'f_g_h'])
        >>> s.str.split('_')
           [a, b, c]
           [c, d, e]
                 NaN
           [f, g, h]
        dtype: object
        >>> s.str.split('_', -1)
           [a, b, c]
           [c, d, e]
                 NaN
           [f, g, h]
        dtype: object
        >>> s.str.split('_', 0)
           [a, b, c]
           [c, d, e]
                 NaN
           [f, g, h]
        dtype: object
        >>> s.str.split('_', 1)
           [a, b_c]
           [c, d_e]
                NaN
           [f, g_h]
        dtype: object
        >>> s.str.split('_', 2)
           [a, b, c]
           [c, d, e]
                 NaN
           [f, g, h]
        dtype: object
        >>> s.str.split('_', 3)
           [a, b, c]
           [c, d, e]
                 NaN
           [f, g, h]
        dtype: object
3、get() 获取指定位置的字符串
        >>> s.str.get(0)
             a
             c
           NaN
             f
        dtype: object
        >>> s.str.get(1)
             _
             _
           NaN
             _
        dtype: object
        >>> s.str.get(2)
             b
             d
           NaN
             g
        dtype: object
4、join() 对每个字符都用给点的字符串拼接起来,不常用
        >>> s.str.join("!")
           a!_!b!_!c
           c!_!d!_!e
                 NaN
           f!_!g!_!h
        dtype: object
        >>> s.str.join("?")
           a?_?b?_?c
           c?_?d?_?e
                 NaN
           f?_?g?_?h
        dtype: object
        >>> s.str.join(".")
           a._.b._.c
           c._.d._.e
                 NaN
           f._.g._.h
        dtype: object
5、contains() 是否包含表达式
        >>> s.str.contains('d')
           False
            True
             NaN
           False
        dtype: object
6、replace() 替换
        >>> s.str.replace("_", ".")
           a.b.c
           c.d.e
             NaN
           f.g.h
        dtype: object
7、repeat() 重复
        >>> s.str.repeat(3)
           a_b_ca_b_ca_b_c
           c_d_ec_d_ec_d_e
                       NaN
           f_g_hf_g_hf_g_h
        dtype: object
8、pad() 左右补齐
>>> s.str.pad(10, fillchar="?")
   ?????a_b_c
   ?????c_d_e
          NaN
   ?????f_g_h
dtype: object
>>>
>>> s.str.pad(10, side="right", fillchar="?")
   a_b_c?????
   c_d_e?????
          NaN
   f_g_h?????
dtype: object
9、center() 中间补齐,看例子
>>> s.str.center(10, fillchar="?")
   ??a_b_c???
   ??c_d_e???
          NaN
   ??f_g_h???
dtype: object
10、ljust() 右边补齐,看例子
>>> s.str.ljust(10, fillchar="?")
   a_b_c?????
   c_d_e?????
          NaN
   f_g_h?????
dtype: object
11、rjust() 左边补齐,看例子
>>> s.str.rjust(10, fillchar="?")
   ?????a_b_c
   ?????c_d_e
          NaN
   ?????f_g_h
dtype: object
12、zfill() 左边补0
>>> s.str.zfill(10)
   00000a_b_c
   00000c_d_e
          NaN
   00000f_g_h
dtype: object
13、wrap() 在指定的位置加回车符号
>>> s.str.wrap(3)
   a_b\n_c
   c_d\n_e
       NaN
   f_g\n_h
dtype: object
14、slice() 按给点的开始结束位置切割字符串
>>> s.str.slice(1,3)
    _b
    _d
   NaN
    _g
dtype: object
15、slice_replace() 使用给定的字符串,替换指定的位置的字符
>>> s.str.slice_replace(1, 3, "?")
   a?_c
   c?_e
    NaN
   f?_h
dtype: object
>>> s.str.slice_replace(1, 3, "??")
   a??_c
   c??_e
     NaN
   f??_h
dtype: object
16、count() 计算给定单词出现的次数
>>> s.str.count("a")
    1
    0
  NaN
    0
dtype: float64
17、startswith() 判断是否以给定的字符串开头
>>> s.str.startswith("a");
    True
   False
     NaN
   False
dtype: object
18、endswith() 判断是否以给定的字符串结束
>>> s.str.endswith("e");
   False
    True
     NaN
   False
dtype: object
19、findall() 查找所有符合正则表达式的字符,以数组形式返回
>>> s.str.findall("[a-z]");
   [a, b, c]
   [c, d, e]
         NaN
   [f, g, h]
dtype: object
20、match() 检测是否全部匹配给点的字符串或者表达式
>>> s
   a_b_c
   c_d_e
     NaN
   f_g_h
dtype: object
>>> s.str.match("[d-z]");
   False
   False
     NaN
    True
dtype: object
21、extract() 抽取匹配的字符串出来,注意要加上括号,把你需要抽取的东西标注上
>>> s.str.extract("([d-z])");
   NaN
     d
   NaN
     f
dtype: object
22、len() 计算字符串的长度
>>> s.str.len()
    5
    5
  NaN
    5
dtype: float64 
23、strip() 去除前后的空白字符
>>> idx pandas.Series([' jack', 'jill ', jesse ', 'frank'])
>>> idx.str.strip()
    jack
    jill
   jesse
   frank
dtype: object
24、rstrip() 去除后面的空白字符
25、lstrip() 去除前面的空白字符
26、partition() 把字符串数组切割称为DataFrame,注意切割只是切割称为三部分,分隔符前,分隔符,分隔符后
27、rpartition() 从右切起
>>> s.str.partition('_')
       2
       b_c
       d_e
 NaN  NaN  NaN
       g_h
>>> s.str.rpartition('_')
       2
 a_b       c
 c_d       e
 NaN  NaN  NaN
 f_g       h
28、lower() 全部小写
29、upper() 全部大写
30、find() 从左边开始,查找给定字符串的所在位置
>>> s.str.find('d')
   -1
    2
  NaN
   -1
dtype: float64
31、rfind() 从右边开始,查找给定字符串的所在位置

32、index() 查找给定字符串的位置,注意,如果不存在这个字符串,那么会报错!
33、rindex() 从右边开始查找,给定字符串的位置
>>> s.str.index('_')
    1
    1
  NaN
    1
dtype: float64
34、capitalize() 首字符大写
>>> s.str.capitalize()
   A_b_c
   C_d_e
     NaN
   F_g_h
dtype: object
35、swapcase() 大小写互换
>>> s.str.swapcase()
   A_B_C
   C_D_E
     NaN
   F_G_H
dtype: object
36、normalize() 序列化数据,数据分析很少用到,咱们就不研究了
37、isalnum() 是否全部是数字和字母组成
>>> s.str.isalnum()
   False
   False
     NaN
   False
dtype: object
38、isalpha() 是否全部是字母
>>> s.str.isalpha()
   False
   False
     NaN
   False
dtype: object
39、isdigit() 是否全部都是数字
>>> s.str.isdigit()
   False
   False
     NaN
   False
dtype: object
40、isspace() 是否空格
>>> s.str.isspace()
   False
   False
     NaN
   False
dtype: object
41、islower() 是否全部小写
42、isupper() 是否全部大写
>>> s.str.islower()
   True
   True
    NaN
   True
dtype: object
>>> s.str.isupper()
   False
   False
     NaN
   False
dtype: object
43、istitle() 是否只有首字母为大写,其他字母为小写
>>> s.str.istitle()
   False
   False
     NaN
   False
dtype: object
44、isnumeric() 是否是数字
45、isdecimal() 是否全是数字

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有