Group operations on Pandas column containing lists

后端 未结 2 515
南笙
南笙 2021-01-18 13:00

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

相关标签:
2条回答
  • 2021-01-18 13:32

    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.

    0 讨论(0)
  • 2021-01-18 13:48

    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]}
    
    0 讨论(0)
提交回复
热议问题