1

I have a DataFrame with the following info

countries    flags
FR           aabb
DE           abc
UK           bbcc
IT           ddd

I want to transform the flags column into a new column that contains only the non-duplicated charts of the flags column. The result should be

countries    flags
FR           ab
DE           abc
UK           bc
IT           d
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2082695
  • 250
  • 3
  • 14

1 Answers1

2

You can use apply with set which remove duplicates, sort them by sorted and last join to string:

df.flags = df.flags.apply(lambda x: "".join(sorted(set(x))))
print (df)
  countries flags
0        FR    ab
1        DE   abc
2        UK    bc
3        IT     d
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252