Home > Software engineering >  Compare elements in lists on 1 column rows and assign unique values in new column, Pandas
Compare elements in lists on 1 column rows and assign unique values in new column, Pandas

Time:11-26

I would like to compare elements in lists in 1 column and assign unique values that are on no other row in new column in the same Pandas df:

Int.:

data = {'object_1':[1, 3, 4, 5, 77],
'object_2':[1, 5, 100, 3, 4],
"object_3": [1, 3, 4, 5, 5],
"object_4": [1, 3, 5, 47, 48]}

Out.:

data = {'object_1':[1, 3, 4, 5, 77],
'object_2':[1, 5, 100, 3, 4],
"object_3": [1, 3, 4, 5, 5],
"object_4": [1, 3, 5, 47, 48],
"unique_values": [[77], [100], [None], [47,48]],
}

Thanks.

CodePudding user response:

You can use isin and stack:

data['unique_values'] = ([df.loc[~df[col].isin(df.set_index(col).stack()), col].tolist()
                   for col in df.columns])

data:

{'object_1': [1, 3, 4, 5, 77],
 'object_2': [1, 5, 100, 3, 4],
 'object_3': [1, 3, 4, 5, 5],
 'object_4': [1, 3, 5, 47, 48],
 'unique_values': [[77], [100], [], [47, 48]]}

CodePudding user response:

Here is a way using drop_duplicates()

df.stack().drop_duplicates(keep=False).groupby(level=1).agg(list).to_dict()

Output:

{'object_1': [77], 'object_2': [100], 'object_4': [47, 48]}
  • Related