Split a column in df by another column value

Andrew Vitek :

In python, I have the following df (headers in first row):

FullName          FirstName
'MichaelJordan'   'Michael'
'KobeBryant'      'Kobe'
'LeBronJames'     'LeBron'  

I am trying to split each record in "FullName" based on the value in "FirstName" but am not having luck...

This is what I tried:

df['Names'] = df['FullName'].str.split(df['FirstName'])

Which produces error:

'Series' objects are mutable, thus they cannot be hashed

Desired output:

print(df['Names'])

['Michael', 'Jordan']
['Kobe', 'Bryant']
['LeBron', 'James']
piRSquared :

str.replace

lastnames = [full.replace(first, '') for full, first in zip(df.FullName, df.FirstName)]
df.assign(LastName=lastnames)

        FullName FirstName LastName
0  MichaelJordan   Michael   Jordan
1     KobeBryant      Kobe   Bryant
2    LeBronJames    LeBron    James

Same exact idea but using map

df.assign(LastName=[*map(lambda a, b: a.replace(b, ''), df.FullName, df.FirstName)])

        FullName FirstName LastName
0  MichaelJordan   Michael   Jordan
1     KobeBryant      Kobe   Bryant
2    LeBronJames    LeBron    James

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=3953&siteId=1