1

I have my pandas dataframe contain data in the following format:

SAC1001.K
KAM10120.B01.W001
CLT004.09C
ASMA104
AJAY101.A.KAS.101

I wish to modify the column using string manipulation so, that the result is

SAC1001.K
KAM10120.B01
CLT004.09C
ASMA104
AJAY101.A

How this can be done? Regex looks to be one way but, not sure of it. Any other elegant way to do it? Please guide

2 Answers 2

2
In [109]: df
Out[109]:
                 col
0          SAC1001.K
1  KAM10120.B01.W001
2         CLT004.09C
3            ASMA104
4  AJAY101.A.KAS.101

In [110]: df['col'] = df['col'].str.replace(r'(\..*?)\..*', r'\1')

In [111]: df
Out[111]:
            col
0     SAC1001.K
1  KAM10120.B01
2    CLT004.09C
3       ASMA104
4     AJAY101.A
Sign up to request clarification or add additional context in comments.

Comments

1

Here is another way without regex but maybe with too many str

df['col'].str.split('.').str[:2].str.join('.')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.