User Tools

Site Tools


str.extract

Extract the 5 digits in a string, always between 2 dashes.

df2 = pd.DataFrame({'Banner': {0: 'banner1', 1: 'banner2', 2: 'banner3', 3: 'banner4', 4: 'banner5'},
                    'Campaign': {0: 'campaign1', 1: 'campaign2', 2: 'none', 3: 'campaign4', 4: 'campaign5'}, 'Country ': {0: 'de', 1: 'it', 2: 'de', 3: 'en', 4: 'en'}, 
                    'Date': {0: '1/1/2016', 1: '1/1/2016', 2: '1/1/2016', 3: '3/1/2016', 4: '4/1/2016'},
                    'Value_2': {0: 5, 1: 10, 2: 15, 3: 20, 4: 25}, 
                    'id':{0:'111-000-12341-00', 1: '111-000-12342-00', 2: '111-000-12345-00', 3: '111-000-12343-00', 4: '111-000-12346-00'}})
 
df2['id_campaign'] = df2['id'].str.extract('[.-]([0-9]{5})[.-]?')

Example 2:

[.-]([0-9]{5})[.-][0-9]+$

Details:

  • [.-] - a . or - separator
  • ([0-9]{5}) - Group 1 capturing 5 digits
  • [.-] - again a separator
  • [0-9]+ -1+ digits
  • $ - end of string


 
  Thanks to the "$“ anchor, the digit groups that are at the end are matched.
str.extract.txt · Last modified: 2016/10/12 11:28 by vincenzo