[Python] Pandas and Sets - ValueError: Length of values does not match length of index

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a new column in my dataframe that contains the intersection of two sets (each contained in two separate columns). The columns themselves hold sets.

    dfc['INTERSECTION'] = set(dfc.TABS1).intersection(set(dfc.TABS2))


    I get a Value error. I was able to do

    dfc['LEFT'] = set(dfc.TABS1) - set(dfc.TABS2)


    no problem. TABS1 and TABS2 have values.

    Any thoughts? Thanks.

    I am adding example data below.

    GROUP TABS1 TABS2
    A {'T1','T2','T3'} {'T2','T3','T4'}
    B {'T5', 'T6'} {'T6'}


    Chris gave example, but using very different data set. I am looking for the intersection of TAB1 and TAB2 in a third column 'INTERSECTION. As mentioned above, I have no problems with

    dfc['LEFT'] = set(dfc.TAB1) - set(dfc.TAB2)


    This looks like it should be so straight forward...

    Continue reading...

Compartilhe esta Página