I need to split the document path to the foldername and the document name in python. It is a large dataframe including many rows.For the filename with no document name followed, just leave the document name column blank in the result. For example, I have a dataframe like the follows:
no filename
1 \\apple\config.csv
2 \\apple\fox.pdf
3 \\orange\cat.xls
4 \\banana\eggplant.pdf
5 \\lucy
...
I expect the output shown as follows:
foldername documentname
\\apple config.csv
\\apple fox.pdf
\\orange cat.xls
\\banana eggplant.pdf
\\lucy
...
I have tried the following code,but it does not work.
y={'Foldername':[],'Docname':[]}
def splitnames(x):
if "." in x:
docname=os.path.basename(x)
rm="\\"+docname
newur=x.replace(rm,'')
else:
newur=x
docname=""
result=[newur,docname]
y["Foldername"].append(result[0])
y["Docname"].append(result[1])
return y;
dff=df$filename.apply(splitnames)
Thank you so much for the help!!