The Python Pandas data box's str column is built into the method detailed __python
Source: Internet
Author: User
Original link: http://www.datastudy.cc/to/27
In the process of using the dataframe of the pandas framework, if you need to handle some character strings, such as determining whether a column contains some keywords, whether a column has a character length of less than 3, and so on, it can be much easier to handle if you master the method built into the STR column.
Let's take a look at the details of what the Str-band method of the series class is.
1, Cat () Stitching string Example: >>> series ([' A ', ' B ', ' C ']). Str.cat ([' A ', ' B ', ' C '], sep= '), ') 0 a,a 1 b,b 2 c,c dtype: object >>> series ([' A ', ') B ', ' C ']). Str.cat (sep= ', ') ' a,b,c ' >>> series ([' A ', ' B ']). Str.cat ([' x ', ' y '], [' 1 ', ') 2 ']], sep= ', ') 0 a,x,1 1 b,y,2 dtyPe: object 2, Split () Shard string >>> import numpy,pandas; >>> s = pandas. Series ([' 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,&nbsP;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 &NBsp; >>> s.str.split (' _ ', 3)      0    [A, B, C] 1 [c, d, e] 2 nan  3    [F, G, H] dtype: Object 3, Get () gets the string at the specified location >>> s.str.get (0) 0 a 1 c 2 nan 3 f &nbsP; dtype: object >>> s.str.get (1) 0 _ 1 _ 2 nan 3 _ dtype: object >>> s.str.get (2) 0 b 1 d 2 nan 3 g Dtype: object 4, join () concatenation of each character with the point string, not commonly used >> > s.str.join ("!") 0 a!_!b!_!c 1 c!_!d!_!e 2 nan 3 f!_!g!_!h dtype: object >>> s.str.join ("?") 0 a?_?b?_?c 1 c?_?d?_?e 2 nan 3 F?_?g?_?h dtype: object >>> s.str.join (".") 0    A._.B._.C         1    C._.D._.E 2 NaN 3 f._.g._.h dtype: object 5, contains () contains an expression >>> s.str.contains (' d ') 0 False 1 true 2 nan 3&nbsP; 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 () repeat >>> s.str.repeat (3) 0 a_b_ca_b_ca_b_c 1 c_d_ Ec_d_ec_d_e 2 &nbSp; nan 3 f_g_hf_g_hf_g_h dtype: object 8, pad () padded >>> 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?????
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.