I want to combine multiple columns of a Pandas DataFrame
into a single column of lists, such that each list does not contain duplicate values and does not contain null values.
So, for example, in the data frame below, columns A
, B
and C
are combined into column D
:
A B C D
0 "KFC" NaN "KFC" ["KFC"]
1 NaN "Mandai Zoo" "Singapore Zoo" ["Mandai Zoo", "Singapore Zoo"]
2 "Tampines Mall" NaN NaN ["Tampines Mall"]
The best I could come up of this was a combination of this and this:
df['D'] = df[['A', 'B', 'C']].values.tolist()
df['D'] = df['D'].map(set).tolist()
which would give me this:
A B C D
0 "KFC" NaN "KFC" ["KFC", NaN]
1 NaN "Mandai Zoo" "Singapore Zoo" [NaN, "Mandai Zoo", "Singapore Zoo"]
2 "Tampines Mall" NaN NaN ["Tampines Mall", NaN]
But that still leaves the NaN
values in the list.