Detailed python pandas string functions (rpm)

 Detailed pandas string functions (turn) - original connections see the end of the article

In using DataFrame pandas frame, if need to address some of the string properties, e.g. a column determines whether or not contain keywords, whether the character length of a column is less than 3, and so this demand, if the master column str built-in method to deal with them a lot easier.

        Let's take a closer look, Series class method which comes str.

1, cat () string concatenation
        example:
        . >>> Series ([ 'A', 'B', 'C']) str.cat ([ 'A', 'B', 'C'], On Sep = ',')
        0 A, A
        . 1 B, B
        2 C, C
        DTYPE: Object
        >>> Series ([ 'A', 'B', 'C']) str.cat (On Sep = ',').
        ' A, B, C '
        >>> Series ([' A ',' B ']). str.cat ([[' X ',' Y '], ['. 1 ',' 2 ']], On Sep = ',')
        0 A, X,. 1
        . 1 B, Y, 2
        DTYPE: Object
2, Split () slicing string
        >>> Import numpy, PANDAS;
        >>> pandas.Series S = ([ 'a_b_c', 'c_d_e', numpy.nan, 'f_g_h'])
        >>> s.str.split('_')
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', -1)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', 0)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', 1)
        0    [a, b_c]
        1    [c, d_e]
        2         NaN
        3    [f, g_h]
        dtype: object
        >>> s.str.split('_', 2)
        0    [a, b, c]
        1    [c, d,E]
        2 NaN
        . 3 [F, G, H]
        DTYPE: Object
        >>> s.str.split ( '_',. 3)
        0 [A, B, C]
        . 1 [C, D, E]
        2 NaN3
        . 3 [F, G, H]
        DTYPE: Object
. 3, GET () Gets the specified location string
        >>> s.str.get (0)
        0 a
        . 1 C
        2 NaN3
        . 3 F
        DTYPE: Object
        >>> s.str.get (. 1)
        0 _
        . 1 _
        2 NaN3
        . 3 _
        DTYPE: Object
        >>> s.str.get (2)
        0 B
        . 1 D
        2 NaN3
        . 3 G
        DTYPE: Object
. 4, the Join () are used for each character string to the point of stitching together, not commonly
        >>> s.str.join ( "!")
        0 _ A _ B C!!!!
        . 1 C! ! _ D _ E!!
        2 NaN3
        . 3 F H _ G _!!!!
        DTYPE: Object
        >>> s.str.join ( "?")
        0 _ A _ B C????
        . 1 C _?? ?? D _ E
        2 NaN3
        ??. 3 F H _ G _??
        DTYPE: Object
        >>> s.str.join ( ".")
        0 ._ A B C ._..
        . 1 D C ._.. _.e
        2 NaN3
        . 3 ._ F G H ._..
        DTYPE: Object
. 5, the contains () contains the expression
        >>> s.str.contains ( 'd')
        0    False
        1     True
        2      NaN
        3    False
        dtype: object
6、replace() 替换
        >>> s.str.replace("_", ".")
        0    a.b.c
        1    c.d.e
        2      NaN
        3    f.g.h
        dtype: object
7、repeat() 重复
        >>> s.str.repeat(3)
        0    a_b_ca_b_ca_b_c
        1    c_d_ec_d_ec_d_e
        2                NaN
        3    f_g_hf_g_hf_g_h
        dtype: object
8、pad() 左右补齐
>>> s.str.pad(10, fillchar="?")
0    ?????a_b_c
1    ?????c_d_e
2           NaN
3    ?????f_g_h
dtype: object
>>>
S.str.pad >>> (10, Side = "right", FillChar = "?")
0 ????? a_b_c
. 1 c_d_e ?????
2 NaN3
. 3 f_g_h ?????
DTYPE: Object
. 9 , Center () filled intermediate, see examples
>>> s.str.center (10, FillChar = "?")
0 ?? a_b_c ???
. 1 ?? c_d_e ???
2 NaN3
. 3 ?? f_g_h ???
DTYPE: Object
10, ljust () filled the right, see example
>>> s.str.ljust (10, FillChar = "?")
0 ????? a_b_c
. 1 c_d_e ?????
2 NaN3
. 3 f_g_h? ????
dtype: Object
11, rjust () left filled, see examples
>>> s.str.rjust (10, fillchar = " ?")
0    ?????a_b_c
1    ?????c_d_e
2           NaN
3    ?????f_g_h
dtype: object
12, zfill () left complement 0
>>> s.str.zfill (10)
0 00000a_b_c
. 1 00000c_d_e
2 NaN3
. 3 00000f_g_h
DTYPE: Object
13 is, wrap () carriage return symbol at the specified position
>>> s.str. wrap (. 3)
0 a_b \ N_C
. 1 C_D \ n_e
2 NaN3
. 3 f_g \ N_H
DTYPE: Object
14, Slice (press start point to the end position of the cutting string)
>>> s.str.slice (l, 3)
0 _B
. 1 _D
2 NaN3
. 3 _g
DTYPE: Object
15, slice_replace () given string, character replacement location designated
>>> s.str.slice_replace (. 1,. 3, "?")
0 a _c?
. 1 ? c _e
2 NaN
3 f _H?
dtype: Object
>>> s.str.slice_replace (1, 3, "??")
0    a??_c
E C ?? _. 1
2 NaN3
. 3 F H ?? _
DTYPE: Object
16, COUNT () is calculated given number of occurrences of the word
>>> s.str.count ( "A")
0. 1
. 1 0
2 NaN3
. 3 0
DTYPE: float64
. 17, startsWith () starts with determining whether a given string
>>> s.str.startswith ( "a");
0 True
. 1 False
2 NaN3
. 3 False
DTYPE: Object
18 is, whether endsWith () determines a given the end of the string
>>> s.str.endswith ( "E");
0 False
1 True
2 NaN
3 False
dtype: Object
19, findAll () to find all the characters in line with the regular expression, returns as an array
>>> s.str.findall ( "[AZ]");
0 [A, B, C]
. 1 [C, D, e]
2          NaN
3    [f, g, h]
DTYPE: Object
20 is, match () detects whether or not all match a string point or expression
>>> S
0 a_b_c
. 1 c_d_e
2 NaN3
. 3 f_g_h
DTYPE: Object
>>> s.str.match ( "[DZ]") ;
0 False
1 False
2 NaN
3 True
dtype: Object
21, extract () string of matches drawn out, pay attention to add brackets, you need to extract something marked on
>>> s.str.extract ( "([ DZ]) ");
0 NaN3
. 1 D
2 NaN3
. 3 F
DTYPE: Object
calculating the length of the string 22 is, len ()
>>> s.str.len ()
0. 5
. 1. 5
2 NaN3
. 3. 5
DTYPE: float64 
23 is, strip () before and after removal of whitespace characters
>>> idx = pandas.Series ([ 'jack ', 'jill', ' jesse ', 'frank'])
Idx.str.strip >>> ()
0 Jack
. 1 Jill
2 Jesse
. 3 Frank
DTYPE: Object
24, The rstrip () later removed whitespace
25, lstrip () removing preceding whitespace
26, partition () the array of strings DataFrame called cutting, but cutting the cut note called three parts, the first separator, the separator, the separator
27, rpartition () cut and raised right
>>> s.str.partition ( '_')
 0. 1 2
0 _ B_c A
. 1 C _ D_E
2 NaN3 NaN3 NaN3
. 3 F _ G_h
>>> s.str.rpartition ( '_')
 0. 1 2
0 _ C a_b
. 1 C_D _ E
2 NaN3 NaN3 NaN3
. 3 f_g _ H
28, Lower () all lower case
29, upper () all caps
30, find () from the left to find the location of a given string
>>> s.str.find ( 'd')
0    -1
1     2
NaN3 2
. 3 -1
DTYPE: float64
31 is, rfind () starting from the right, to find the location of a given string

32, index () to find the position of a given string, note that if the string does not exist, then will complain!
33, rindex () from the right to find, position of a given string
>>> s.str.index ( '_')
0. 1
. 1. 1
2 NaN3
. 3. 1
DTYPE: float64
34 is, capitalize () uppercase first character
>> > s.str.capitalize ()
0 A_b_c
. 1 C_d_e
2 NaN3
. 3 F_g_h
DTYPE: Object
35, swapcase () invert case
>>> s.str.swapcase ()
0 A_B_C
. 1 C_D_E
2 NaN3
. 3 F_G_H
DTYPE: Object
36 , the normalize () serialized data, rarely used data analysis, we will not study
37, isalnum () whether all numbers and letters
>>> s.str.isalnum ()
0 False
. 1 False
2 NaN3
. 3 False
dtype: object
38, isalpha () whether all the letters
>>> s.str.isalpha ()
0 False
. 1 False
2 NaN3
. 3 False
DTYPE: Object
39, isdigit () whether or not all are digital
>>> s.str.isdigit ()
False 0
. 1 False
2 NaN3
. 3 False
DTYPE: Object
40, isspace becomes () whether space
>>> s.str.isspace ()
0 False
. 1 False
2 NaN3
. 3 False
DTYPE: Object
41 is, islower () whether all lower case
42, isupper () Are all uppercase
>>> s.str.islower ()
0 True
1 True
2 NaN
3 True
dtype: Object
>>> s.str.isupper ()
0 False
1 False
2 NaN
False 3
dtype: Object
43, istitle () only if the first letter is capitalized other letters to lowercase
>>> s.str.istitle ()
0 False
1 False
2 NaN
3 False
dtype: Object
44, IsNumeric () whether it is digital
45, isdecimal () whether all digital
---------------------
author: big data Analytics combat
source: CSDN
original: https: //blog.csdn.net / qq_28219759 / article / details / 52919233
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/keeptg/p/11057621.html