I have a DataFrame containing a column, props
, which contains lists of strings.
Ideally, I\'d like to group by this column, but I predictably get an err
You can convert the lists of strings into tuples of strings. Tuples are hashable, as they are unmutable. This is of course assuming that you don't need to be adding to or removing from those lists after creation.
You can use the immutable counterpart to lists, which are tuples:
>>> import pandas as pd
>>> df = pd.DataFrame([[[1, 2], 'ab'], [[2, 3], 'bc']])
>>> df.groupby(0).groups
...
... TypeError: unhashable type: 'list'
You could apply the conversion on the appropriate column:
>>> df[0] = df[0].apply(tuple)
>>> df.groupby(0).groups
{(1, 2): [0], (2, 3): [1]}