pandas: merge on column of collections.Counter (or even just dict) objects?

后端 未结 1 775
别那么骄傲
别那么骄傲 2021-01-24 18:22

I need to perform a merge of two pandas DataFrames using columns with collections.Counter objects (https://docs.python.org/2/library/collections.html#collections.Co

相关标签:
1条回答
  • 2021-01-24 19:18

    One way of getting around this is to create a column (for each DataFrame) of a version of your original data structure converted to a hashable type.

    E.g.,

    a['IDHash'] = a.ID.apply(lambda r: tuple(sorted(r.iteritems())))
    b['IDHash'] = b.ID.apply(lambda r: tuple(sorted(r.iteritems())))
    

    and then

    pd.merge(a, b, on='IDHash')
    

    After that, just erase the columns.

    0 讨论(0)
提交回复
热议问题