Home > Net >  Pandas dataframe new column from everything after the last backslash
Pandas dataframe new column from everything after the last backslash

Time:11-09

I have the following dataframe.

ID Location
1 C:\Users\user1\Documents\fb_working\Testdata\Patterns\NLP Patterns
2 C:\Users\user1\Documents\fb_working\Testdata\Patterns\NLP Patterns

I want to create a new column with everything after the last backslash in the location column as shown below

ID Location New_Location
1 C:\Users\user1\Documents\fb_working\Testdata\Patterns\NLP Patterns NLP Patterns
2 C:\Users\user1\Documents\fb_working\Testdata\Patterns\NLP Patterns NLP Patterns

How can I do this? I'm having trouble using backslash as a delimiter

CodePudding user response:

You can use Series.str.split()

df["New_Location"] = df["Location"].str.split(pat="\\", n=7, expand=False).str[-1]
  • Related