2018.03.26 Python-pandas String Common methods

Source: Internet
Author: User
Tags first string

 import NumPy as NP
Import Pandas as PD
1 #string Common methods-strip2s = PD. Series (['Jack','Jill','Jease','Feank'])3DF = PD. DataFrame (Np.random.randn (3,2), columns=['Column A','Column B'],index=range (3))4 Print(s)5 Print(df.columns)6 7 Print('----')8 Print(S.str.lstrip (). Values)#Remove the left space9 Print(S.str.rstrip (). Values)#Remove the space on the rightTenDf.columns =Df.columns.str.strip () One Print(Df.columns)

Results:

0      Jack 1       jill2     jease 3      feankdtype:objectindex ([' Column A ', ' column B '], dtype= ' object ')----[' Jack ' J Ill ' jease ' feank ' ['  Jack ' Jill ' Jease ' Feank ']index ([' Column A ', ' column B '], dtype= ' object ')
#string Common Method-replace replacement stringDF = PD. DataFrame (Np.random.randn (3,2), columns=['Columns A','Columns B'],index = Range (3))Print(df.columns) df.columns= Df.columns.str.replace (' ','-')Print(df.columns) df.columns= Df.columns.str.replace ('-','hehe', n=1)#to replace the first one with hehe.Print(Df.columns)

Results:

Index ([' Columns A ', '  Columns B '], dtype= ' object ') index (['-columns-a ', '--columns-b '], dtype= ' object ') index ([' Hehecolumns-a ', ' hehe-columns-b '], dtype= ' object ')
#string Common methods-split, rsplit in the form of lists lists = PD. Series (['A,b,c',' the',['a,,, C'],np.nan])Print(s)Print('----')Print(S.str.split (','))Print('----')#split, similar to stringPrint(S.str.split (',') [0])#Index first RowPrint(S.str.split (','). Str[0])#first columnPrint(S.str.split (','). Str.get (1))#second column#You can use the get or [] symbol to access the elements of a split listPrint(S.str.split (',', expand=true,n=1))#N to expand the quantityPrint(S.str.rsplit (',', expand=true,n=1))#Rsplit points from right to left#Expand can extend this operation to return Dataframe#n parameter limit score#Rsplit is similar to split, which works in reverse, from the end of a string to the beginning of a stringPrint('Dataframe:') DF= PD. DataFrame ({'Key1':['A,b,c',' the',[',,,']],                   'Key2':['a-b-c','1-2-c',[',-,-,']]})Print(df['Key2'])Print(df['Key2'].str.split ('-'))

Results:
0 A,b,c
1
2 [a,,, C]
3 NaN
Dtype:object
----
0 [A, b, c]
1 [1, 2, 3]
2 NaN
3 NaN
Dtype:object
----
[' A ', ' B ', ' C ']
0 A
1 1
2 NaN
3 NaN
Dtype:object
0 b
1 2
2 NaN
3 NaN
Dtype:object
0 1
0 A B,c
1 1 2,3
2 Nan Nan
3 Nan Nan
0 1
0 A, b C
1 3
2 Nan Nan
3 Nan Nan
Dataframe
0 a-b-c
1 1-2-c
2 [,-,-,]
Name:key2, Dtype:object
0 [A, b, c]
1 [1, 2, C]
2 NaN
Name:key2, Dtype:object

#String Indexs = PD. Series (['A','b','C','Bbhello','123', Np.nan,'HJ']) DF= PD. DataFrame ({'Key1': List ('abcdef'),                   'Key2':['Hee','FV','W','Hjja','123', Np.nan]})Print(S,'\ n-----')Print(S.str[0])#take the first stringPrint(S.str[:2])#take the first 2 charactersPrint('-----')Print(df['Key2'].str[0])#STR follows the same index as the string itself

Results:

0          A1          B2          C3    Bbhello4        1235        NaN6         hjdtype:object-----0      A1      B2      C3      B4      the      hdtype:object0      A1      B2 C3 Bb4 NaN6 hjdtype:object     -----0      H1 NaN6      F2      W3      h4    Nanname:key2, Dtype:object

2018.03.26 Python-pandas String Common methods

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.