[Python] Get column names where its value contains something in pandas

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with various columns, three of which are columns with lists (each cell has a list). These three columns have mutually exclusive values.

    I have another column which has the label (A, B, C, D or E)

    vot_in_favour vot_against vot_abstention label
    0 [A, B, C] [] [D, E] A
    1 [A, D, E] [C] C
    2 [B, C] [A] [D, E] D


    I want a column vote which has the name of the column according to the label, like the following:

    vote
    0 vot_in_favour
    1 vot_against
    2 vot_abstention


    I tried something like df1['vote'] = df.drop("label", axis=1).isin(df["label"]).any(1), but do not know how to make this to match any value of the lists. I have visited similar issues, but the list columns are posing a challenge.

    Continue reading...

Compartilhe esta Página