Reverse pandas string / reverse extract pandas DF

97 views Asked by At

Good day,

I got files with path in column thus:

pd.DataFrame({'path':[
'C:/some_1_path/file_1.zip',
'C:/some_1_path/file_2.zip']

I wish to extract the pattern _\d from this like this:

'C:/some_1_path/file_1.zip'| '_1' 
'C:/some_1_path/file_2.zip'| '_2'

Since the _1 happens to be in path .str.extract() picks that one. extractall will work , but requires extra steps.

How would one do .str.extract(pattern) in reverse order? I could reverse the string but extra steps and all that.

2

There are 2 answers

1
Tim Biegeleisen On BEST ANSWER

You may use a negative lookahead to ensure the last underscore:

df["val"] = df["path"].str.extract(r'(_\d+)(?!.*_\d+)')
0
jqurious On

You can use .* in your pattern so that you only get the last match:

>>> df['path'].str.extract(r".*(_\d+)")
    0
0  _1
1  _2