1

I have a dataframe with duplicate values in either list or string format.

df = Name                  Email                            years        score 
     john           [[email protected],[email protected], [email protected]]                8           good
               
     
     [devan,smith ,devan]   [[email protected]]                   [8,6,8]           good

I want to remove duplicate values within that particular cell, not to compare corresponding to different cells.

df_updated = Name                  Email                      years        score
             john           [[email protected],[email protected]]                 8            good
               
     
          [devan,smith]          [[email protected]]                   [8,6]         good
1
  • Your input data is ambiguous, please provide it as dataframe or dictionary Use df.to_dict('list') and update your question Commented Feb 18, 2022 at 9:48

2 Answers 2

1

Use DataFrame.applymap for elementwise processing with custom function for remove duplicates if type is list:

df = pd.DataFrame({'Name':['John', ['aa','devan','smith','devan']],
                   'years':[8, [8,6,8]]})

print (df)
                        Name      years
0                       John          8
1  [aa, devan, smith, devan]  [8, 6, 8]

df1 = df.applymap(lambda x: list(dict.fromkeys(x)) if isinstance(x, list) else x)
print (df1)
                 Name   years
0                John       8
1  [aa, devan, smith]  [8, 6]

If ordering is not important use sets:

df2 = df.applymap(lambda x: list(set(x)) if isinstance(x, list) else x)
print (df2)
                 Name   years
0                John       8
1  [devan, smith, aa]  [8, 6]
Sign up to request clarification or add additional context in comments.

4 Comments

looks like dupe ;) I thought of the exact same solution and I imagined it already existed
@mozway - hmm, partly.
I would say fully "After some discussion solution is: df['newlist'] = df['text_lemmatized'].map(lambda x: list(set(x)))" except map in place of applymap and set instead of dict.from_keys, but you also showed those in the dupe
@mozway - ya, partly dupe. part solution is same, part not.
0

Without the main dataframe, it is hard to guess how your dataframe functions. Anyway, here is what you probably need:

df["Email"].apply(set)

Note that Email column should be list. If you are interested in removing duplicated from other columns, let's say Name column, try replacing Name in place of Email in the abovementioned cell.

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.