0

I have a dataframe that looks like below:

Name String Names
First ['A','A','B','C']
Second ['AA','BB']

I want the output to be like

Name String Names
First 'A','B','C'
Second 'AA','BB'

I need the list to be converted to str . I've done that using the below code

df['String Names'] = df['String Names'].str.join(',')

But I'm unable to get only the unique values.

Any suggestion how I can do it in python?

2 Answers 2

1

You might harness pandas.unique function following way:

import pandas as pd
df = pd.DataFrame({'Name':['First','Second'],'String Names':[['A','A','B','C'],['AA','BB']]})
df['String Names'] = df['String Names'].apply(pd.unique).str.join(',')
print(df)

output:

     Name String Names
0   First        A,B,C
1  Second        AA,BB

pandas.unique does preserve order.

Sign up to request clarification or add additional context in comments.

Comments

0

If order is not important convert values to sets:

df['String Names'] = df['String Names'].apply(set).str.join(',')

If order and performance is important use dict trick for remove duplicates:

df['String Names'] = df['String Names'].apply(lambda x: ','.join(dict.fromkeys(x).keys()))

df['String Names1'] = df['String Names'].apply(set).str.join(',')
df['String Names2'] = df['String Names'].apply(lambda x: ','.join(dict.fromkeys(x).keys()))
print(df)

     Name  String Names String Names1 String Names2
0   First  [A, A, B, C]         B,A,C         A,B,C
1  Second      [AA, BB]         AA,BB         AA,BB

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.