[Python] How to explode the column value without duplicating the other columns values in panda...

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have df like this:

    id ColumnA ColumnB ColumnC
    1 Audi_BMW_VW BMW_Audi VW
    2 VW Audi Audi_BMW_VW


    I want to explode the columns based on explode when _ appear. For example for "Column A" like this

    df['Column A'].str.split('_')).explode('Column A')


    but when i use similar query for column B then it repeats the values of column A, but i really want that only ID should duplicate. The desired output would be something like this:

    id ColumnA ColumnB ColumnC
    1 Audi BMW VW
    1 BMW Audi
    1 VW
    2 VW Audi Audi
    2 BMW
    2 VW

    Continue reading...

Compartilhe esta Página