Sort lists in a Pandas Dataframe column
You can also sort values by column.
Example:
x = [['a', 'b'], ['b', 'a'], ['a', 'c'], ['c', 'a']]
df = pandas.DataFrame({'a': Series(x)})
df.a.sort_values()
a
0 [a, b]
2 [a, c]
1 [b, a]
3 [c, a]
However, for what I understand, you want to sort [b, a]
to [a, b]
, and [c, a]
to [a, c]
and then set
values in order to get only [a, b][a, c]
.
i'd recommend use lambda
Try:
result = df.a.sort_values().apply(lambda x: sorted(x))
result = DataFrame(result).reset_index(drop=True)
It returns:
0 [a, b]
1 [a, c]
2 [a, b]
3 [a, c]
Then get unique values:
newdf = pandas.DataFrame({'a': Series(list(set(result['a'].apply(tuple))))})
newdf.sort_values(by='a')
a
0 (a, b)
1 (a, c)
list are unhashable. however, tuples are hashable
use
df.groupby([df.a.apply(tuple)])
setup
df = pd.DataFrame(dict(a=[list('ab'), list('ba'), list('ac'), list('ca')]))
results
df.groupby([df.a.apply(tuple)]).size()
a
(a, b) 1
(a, c) 1
(b, a) 1
(c, a) 1
dtype: int64