-2

I have a dataframe (below), for the column 'name', I want to delete the (for example the first row)

'/Users/xccxken/Desktop/NNRelease/paperVersion/'

and

'.txt'

just keep the word like (example of first row)

'Topic+Topic_of_Situation.shortageglut'

in each line

,n_1,n_2,name
0,water,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
1,supply,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
2,skill,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
214,income,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
215,immigration,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
216,health,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
485,license,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
486,lease,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
487,immunity,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
488,franchise,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
Lyu Keting
  • 83
  • 3
  • 8

1 Answers1

0

You can use the .str.strip() methods:

prefix = '/Users/xccxken/Desktop/NNRelease/paperVersion/'
suffix = '.txt'
df['name'] = df['name'].str.rstrip(suffix).str.lstrip(prefix)

Or regular expressions:

description = r'([^/]+)\.txt'
df['name'] = df['name'].str.extract(description)
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • thanks. can you tell me if i want to extract 'Topic+Topic_of_Situation.othersituation' to 'Topic+Topic_of_Situation',how to write this 'description2 ' . 'Topic+Topic_of_Situation' to 'Topic',how to write 'description3',thanks! – Lyu Keting Dec 14 '16 at 23:19
  • You have several different patterns in your frame. You may want to read more about Python regular expressions (https://docs.python.org/3/library/re.html) and play with them online to find the expression that works: https://regex101.com/ – DYZ Dec 14 '16 at 23:24