How would I extract any word after second hyphen in pandas

Below is my DF

df = pd.DataFrame({'A': ['a', 'b', 'c', '0'],
               'B': ['0xF188-abc-cde', '0xF188-abc-abcde', '0xF188-abc-1234', '0xF188-abc-tu231er']})

Now I want to add NEW column “EXTRACT” which is an extraction of column ‘B’ after second hyphen.

Below is the Expected Column.

df= pd.DataFrame({'A': ['a', 'b', 'c', '0'],
               'B': ["0xF188-abc-cde", '0xF188-abc-abcde', '0xF188-abc-1234', '0xF188-abc-tu231er'],
                    'Extract':['cde', 'abcde', '1234', 'tu231er']})

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First apply a function to split the column B using - and then extract the last element using [-1]:

df['Extract'] = df['B'].apply(lambda x: x.split('-')[-1])

Output:

   A                   B  Extract
0  a      0xF188-abc-cde      cde
1  b    0xF188-abc-abcde    abcde
2  c     0xF188-abc-1234     1234
3  0  0xF188-abc-tu231er  tu231er

Another method as suggested by @sophocles :

df['Extract'] = df.B.str.split('-').str[-1]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x