Search code examples
pythonpandasconcatenationunique

Create a new column with unique by concatenation of 3 column


I want to create below final concate_col by concatenation of col1, col2 and col3 in python

Col1 col2 col3 concate_col
a     b    b      a,b
c     a    a      c,a
d     a    e      d,a,e

I am able to concatenate these 3 column but is having in getting unique values in each rows.

I have used df.str.cat to concatenate


Solution

  • import pandas as pd
    
    
    data = {'col1': ['a', 'c', 'd'],
            'col2': ['b', 'a', 'a'],
            'col3': ['b', 'a', 'e']}
    df = pd.DataFrame(data)
    
    # Concatenate unique values of col1, col2, and col3 for each row
    df['concate_col'] = df.apply(lambda row: ','.join(set(row)), axis=1)
    
    print(df)
    

    Result:

      col1 col2 col3 concate_col
    0    a    b    b         b,a
    1    c    a    a         c,a
    2    d    a    e       d,e,a