Reputation: 165
so I've data like this:
Id Title Fname lname email
1 meeting with Jay, Aj Jay kay [email protected]
1 meeting with Jay, Aj Aj xyz [email protected]
2 call with Steve Steve Jack [email protected]
2 call with Steve Harvey Ray [email protected]
3 lunch Mike Mil Mike [email protected]
I want to remove firstname & last name for each unique Id from Title. I tried grouping by Id which gives series Objects for Title, Fname, Lname,etc
df.groupby('Id')
I've concatenated Fname with .agg(lambda x: x.sum() if x.dtype == 'float64' else ','.join(x))
& kept in concated
dataframe.
likewise all other columns get aggregated. Question is how do I replace values in Title based on this aggregated series.
concated['newTitle'] = [ concated.Title.str.replace(e[0]).replace(e[1]).replace(e[1])
for e in
zip(concated.FName.str.split(','), concated.LName.str.split(','))
]
I want something like this, or some other way, by which for each Id, I could get newTitle, with replaced values.
output be like:
Id Title
1 Meeting with ,
2 call with
3 lunch
Upvotes: 2
Views: 222
Reputation: 38425
Create a mapper series by joining Fname and lname and replace,
s = df.groupby('Id')[['Fname', 'lname']].apply(lambda x: '|'.join(x.stack()))
df.set_index('Id')['Title'].replace(s, '', regex = True).drop_duplicates()
Id
1 meeting with ,
2 call with
3 lunch
Upvotes: 3