简体   繁体   中英

How to remove portion of string after certain character for each element of a Pandas series (or list)?

I have a Pandas series like ['AAA.B', 'BBB.C', 'CCC.D']. I want to remove the portion of each string after the period, inclusive. In other words, the desired result here would be ['AAA', 'BBB', 'CCC']. I can't figure out how to do it without iterating through each element one at a time to operate on them individually, which would be really inefficient. Any idea of how to do this?

Using str.split

Ex:

s = pd.Series( ['AAA.B', 'BBB.C', 'CCC.D'])
print(s.str.split(".").str[0])

Output:

0    AAA
1    BBB
2    CCC
dtype: object

You can use str.replace :

s = pd.Series(['AAA.B', 'BBB.C', 'CCC.D'])
s.str.replace(r'(\.\w+)', '')

0    AAA
1    BBB
2    CCC
dtype: object

The best way of doing this is

s = s.split('.')[0]

Another way can be regex. Or, if the part after '.'also has to be used, do

head, sep, tail = s.partition('.')

In this, head will contain the string portion before '.', sep will contain '.', and tail will contain the portion after '.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM