简体   繁体   中英

Modify series from other series objects

so I've data like this:

Id  Title                   Fname   lname   email
1   meeting with Jay, Aj    Jay     kay     jk@something.com
1   meeting with Jay, Aj    Aj      xyz     aj@something.com
2   call with Steve         Steve   Jack    st@something.com
2   call with Steve         Harvey  Ray     h@something.com
3   lunch Mike              Mil     Mike    m@something.com

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 

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM