Home > Mobile >  Spliting a path column in python
Spliting a path column in python

Time:01-18

Hi I have a column with path like this:

path_column = ['C:/Users/Desktop/sample\\1994-QTR1.tsv','C:/Users/Desktop/sample\\1995-QTR1.tsv']

I need to split and get just the file name.

Expected output:

[1994-QTR1,1995-QTR1]

Thanks

CodePudding user response:

Use enter image description here

CodePudding user response:

Use this or you can use regex to match and take what you want.

path.split("\\")[-1].split(".")[0]

Output:

'1994-QTR1'

Edit

new_col=[]
for i in path_column:
    new_col.append(i.split("\\")[-1].split(".")[0])
print (new_col)

NOTE: If you need it in a list, you can append it to a new list from the loop.

Output:

['1994-QTR1', '1995-QTR1']

CodePudding user response:

You might harness pathlib for this task following way

import pathlib
import pandas as pd
def get_stem(path):
    return pathlib.PureWindowsPath(path).stem
df = pd.DataFrame({'paths':['C:/Users/Desktop/sample\\1994-QTR1.tsv','C:/Users/Desktop/sample\\1994-QTR2.tsv','C:/Users/Desktop/sample\\1994-QTR3.tsv']})
df['names'] = df.paths.apply(get_stem)
print(df)

gives output

                                   paths      names
0  C:/Users/Desktop/sample\1994-QTR1.tsv  1994-QTR1
1  C:/Users/Desktop/sample\1994-QTR2.tsv  1994-QTR2
2  C:/Users/Desktop/sample\1994-QTR3.tsv  1994-QTR3
  • Related