Search code examples
pythonpandascombinationspython-itertools

Get combinations based on a specific column of dataframe in python


I have a dataframe with 3 columns: equivalences, class, ch. I am using Python.

equivalences                             class                                              ch

ETICA CONTABIL                           A ÉTICA CONTÁBIL                                   40.0
ETICA CONTABIL                           A ÉTICA CONTÁBIL COM ENFOQUE                       40.0
BANCO DE DADOS                           GERENCIANDO SEU BD                                 40.0
AMBIENTE WEB                             APLICAÇÕES EM NUVENS                               40.0
AMBIENTE WEB                             ALTA DISPONIBILIDADE                               40.0
TECNOLOGIAS WEB                          PÁGINAS PARA INTERNET                              40.0
TECNOLOGIAS WEB                          PROGRAMAÇÃO WEB AVANÇADA                           40.0
TECNOLOGIAS WEB                          DESENVOLVENDO COM JS                               40.0
None                                     PROGRAMAÇÃO WEB                                    40.0

I need to get the pair combinations of equivalences, summing the ch of this pair. It should be something like this:

equivalences      class a                   class b                                  ch

ETICA CONTABIL    A ÉTICA CONTÁBIL          A ÉTICA CONTÁBIL COM ENFOQUE            80.0
BANCO DE DADOS    GERENCIANDO SEU BD        (null)                                  40.0
AMBIENTE WEB      APLICAÇÕES EM NUVENS      ALTA DISPONIBILIDADE                    80.0
TECNOLOGIAS WEB   PÁGINAS PARA INTERNET     PROGRAMAÇÃO WEB AVANÇADA                80.0
TECNOLOGIAS WEB   PÁGINAS PARA INTERNET     DESENVOLVENDO COM JS                    80.0
TECNOLOGIAS WEB   PROGRAMAÇÃO WEB AVANÇADA  DESENVOLVENDO COM JS                    80.0
(null)            PROGRAMAÇÃO WEB           (null)                                  40.0

I think I would have to use combinations itertools, but I have no clue how i group by equivalences to get distinct pairs. How can I do that?


Solution

  • Here's a solution (in a few steps for clarity):

    # create a cross product of classes per "equivalences"
    t = pd.merge(df.assign(dummy = 1), df.assign(dummy=1), 
             on = ["dummy", "equivalences"])
    
    # drop items in which the left and the right class are identical
    t = t[t.class_x != t.class_y]
    
    # drop duplicates such as x,y vs y,x
    t.loc[t.class_x > t.class_y, ["class_x", "class_y"]] = \
        t.loc[t.class_x > t.class_y, ["class_x", "class_y"]].rename(columns = {"class_x": "class_y", "class_y": "class_x"})
    t = t.drop_duplicates(subset = ["equivalences", "class_x", "class_y"])
    
    
    t["ch"] = t.ch_x + t.ch_y
    res = t.drop(["ch_x", "dummy", "ch_y"], axis=1)
    print(res) 
    
    ==>
    
           equivalences                   class_x                       class_y    ch
    1    ETICA CONTABIL          A ÉTICA CONTÁBIL  A ÉTICA CONTÁBIL COM ENFOQUE  80.0
    6      AMBIENTE WEB      ALTA DISPONIBILIDADE          APLICAÇÕES EM NUVENS  80.0
    10  TECNOLOGIAS WEB  PROGRAMAÇÃO WEB AVANÇADA         PÁGINAS PARA INTERNET  80.0
    11  TECNOLOGIAS WEB      DESENVOLVENDO COM JS         PÁGINAS PARA INTERNET  80.0
    14  TECNOLOGIAS WEB      DESENVOLVENDO COM JS      PROGRAMAÇÃO WEB AVANÇADA  80.0