1

I have 4 columns ( Name, Area, State, Email). In column Email has duplicate email after i group by and join the value. The output turns out to be:

Area    Group   Shift         Email
KM      MFG     A1,A2,A3      [email protected],[email protected],[email protected]
RIM     TEST    A1,B1         [email protected],[email protected],[email protected]
PNG    FINISH   C1,C2,C3      [email protected],[email protected],[email protected]
KL     Manager  NORMAL,B1,B1  [email protected],[email protected],[email protected],[email protected]

I need to remove the duplicate email in column 'Email'. Anyone can help me?

2
  • Please show us your grouping code so we can tell you what to change to remove duplicates. Commented Jun 1, 2020 at 3:19
  • df = df.groupby(['Area','Group])['Email'].apply(','.join).reset_index() Commented Jun 1, 2020 at 9:21

2 Answers 2

1

IIUC, you need this:

df['Email'] = df['Email'].str.split(',').apply(lambda x: ','.join(set(x)))

Output:

  Area    Group         Shift                                Email
0   KM      MFG      A1,A2,A3              [email protected],[email protected]
1  RIM     TEST         A1,B1              [email protected],[email protected]
2  PNG   FINISH      C1,C2,C3              [email protected],[email protected]
3   KL  Manager  NORMAL,B1,B1  [email protected],[email protected],[email protected]
Sign up to request clarification or add additional context in comments.

Comments

1

Let us try

df.Email=df.Email.str.split(',').apply(set).str.join(',')
df
  Area    Group         Shift                                Email
0   KM      MFG      A1,A2,A3              [email protected],[email protected]
1  RIM     TEST         A1,B1              [email protected],[email protected]
2  PNG   FINISH      C1,C2,C3              [email protected],[email protected]
3   KL  Manager  NORMAL,B1,B1  [email protected],[email protected],[email protected]

1 Comment

You too.. I am rusty... you have a better answwer. :)

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.