Amie Johnson
Amie Johnson

Reputation: 57

Python/Pandas remove the start of a string which contains a specific string

I have the following data:

COLUMN NAME
Sample\Sample_ID\
Sample\Sample_ID\
Sample\Sample_ID\
Sample\Sample_ID\
Sample_ID\
Sample_ID\

I want remove/strip "Sample\" for the lines of data which contain this.

Upvotes: 1

Views: 60

Answers (1)

Erfan
Erfan

Reputation: 42916

df['COLUMN NAME'] = df['COLUMN NAME'].str.replace(r'Sample\\', '')

Output

    COLUMN NAME
0   Sample_ID\
1   Sample_ID\
2   Sample_ID\
3   Sample_ID\
4   Sample_ID\
5   Sample_ID\

Note

I use double baslash \\ since a single backslash is an escape character in Python. So we need to use two to literally point to a single backslash.

Upvotes: 1

Related Questions