Merge lists in columns in pandas dataframe

Question:

I’ve got a dataframe with lists in two columns. It looks like this:

    column1    column2             column3
0   text       [cat1,cat2,cat3]    [1,2,3]
1   text2      [cat2,cat3,cat1]    [4,5,6]

The values in column3 belong to the categories in column2. How can I get a dataframe that looks like this?

    column1    cat1     cat2      cat3
0   text         1        2         3
1   text2        6        4         5

Thank you for your help!

Asked By: Mauritz

||

Answers:

You could use explode to break the values in your lists into separate rows and use pivot_table:

df.explode(
    ['column2','column3']
    ).pivot_table(index='column1',columns='column2',values='column3',aggfunc='first').reset_index()

prints:

index     column1 cat1 cat2 cat3
0          text    1    2    3
1         text2    6    4    5
Answered By: sophocles
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.