In python how to capture a value from a column and show in another column of same csv

Viewed 26

I am new to Python and trying to use str.split to capture a folder name from it's path, but it doesn't work.

Original csv file:

Date             Path
01-01-2020       \\A\path1\folder1\subfolder2\file1
01-02-2020       \\A\path1\folder2\subfolder3\file4
01-03-2020       \\A\path1\folder3\subfolder1\file5

I want to capture folder name to another column like below

Date             Path                                        Folder
01-01-2020       \\A\path1\folder1\subfolder2\file1          folder1
01-02-2020       \\A\path1\nameoffolder2\subfolder3\file4    nameoffolder2
01-03-2020       \\A\path1\folder3\subfolder1\file5          folder3
1 Answers

assuming it is always in th 4th index you could:

df['folder'] = [x.split('\\')[4] for x in df['Path']]
Related