1

My input:

df=(pd.DataFrame({'items':['foo','bar','xyz'],
                  'path':['c/folderOne/folderTwo/folderThree','c/folder1/folder2/folder3','c/folderO/folderT/folderTh'],
}))

I trying insert in column path some "fix" after 'c/... I want add new substring such as NEWPATH into list and re-join again, but I ran unexpected problem.
That my code:

df['path'] = df['path'].map(lambda x: x.split("/"))
df['path'] = df['path'].map(lambda x: x.insert(1,'NEWPATH'))
df['path'] = df['path'].map(lambda x: x.join('/'))

Code return None in column path, when I expected something like this, after second line of my code:
in column df['path'] : ['c','NEWPATH','folderOne'....] etc. for each row(cell) of my column.

3 Answers 3

1

Looks like you should better use a regex here:

NEWPATH = 'abc'
df['path2'] = df['path'].str.replace('^([^/]+/)', rf'\1{NEWPATH}/')

NB. assigning to a new column here for clarity

output:

  items                               path                                  path2
0   foo  c/folderOne/folderTwo/folderThree  c/abc/folderOne/folderTwo/folderThree
1   bar          c/folder1/folder2/folder3          c/abc/folder1/folder2/folder3
2   xyz         c/folderO/folderT/folderTh         c/abc/folderO/folderT/folderTh
inserting after the nth directory:
NEWPATH = 'abc'
df['path2'] = df['path'].str.replace('^(^(?:[^/]+/){3})', rf'\1{NEWPATH}/')

or

NEWPATH = 'abc'
N = 3
df['path2'] = df['path'].str.replace(f'^(^(?:[^/]+/){{{N}}})', rf'\1{NEWPATH}/')

output:

  items                               path                                  path2
0   foo  c/folderOne/folderTwo/folderThree  c/folderOne/folderTwo/abc/folderThree
1   bar          c/folder1/folder2/folder3          c/folder1/folder2/abc/folder3
2   xyz         c/folderO/folderT/folderTh         c/folderO/folderT/abc/folderTh
Sign up to request clarification or add additional context in comments.

Comments

1

Problem is insert working inplace, so None is returned. You can add list to splitted x and then join:

def f(x):
    s = x.split("/")
    return '/'.join(s[:2] + ['NEWPATH'] + s[2:])
    
df['path'] = df['path'].map(f)

print (df)
  items                                       path
0   foo  c/folderOne/NEWPATH/folderTwo/folderThree
1   bar          c/folder1/NEWPATH/folder2/folder3
2   xyz         c/folderO/NEWPATH/folderT/folderTh

2 Comments

Thanks, but how add 'NEWPATH' as third item i.e. c/folderOne/NEWPATH/... ?
@TeoK - df['path'] = df['path'].map(lambda x: '/'.join(x.split("/")[:2] + ['NEWPATH'] + x.split("/")[2:]))
0

You can use a map as well - the index 1 in the lambda here indicates the position where you want to insert

df['path'] = df['path'].str.split('/').map(lambda x: x[:1] + ['NEWPATH'] + x[1:]).str.join('/')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.