Extract values from a column value containing a list of values

Question:

I have a column containing values that also contains a list of values. E.g,

actor = ["[Emil Eifrem,Hugo Weaving,Laurence Fishburne]"]
title = ["The Matrix"]

actors = pd.DataFrame(list(zip(actor, title)),
                      columns = ["actor", "title"])

The output that I am looking for is below,

actor = ["Emil Eifrem", "Hugo Weaving", "Laurence Fishburne"]
title = ["The Matrix"] * 3

actors = pd.DataFrame(list(zip(actor, title)),
                      columns = ["actor", "title"])
Asked By: Salahuddin

||

Answers:

Your value in actor column is a string. You may use below to convert to a list first and then use explode.

actors.actor = actors.actor.str.strip('[]').str.split(',')
actors = actors.explode('actor', ignore_index=True)

print(actors):

                 actor       title
0          Emil Eifrem  The Matrix
1         Hugo Weaving  The Matrix
2   Laurence Fishburne  The Matrix
Answered By: SomeDude

Generic solution even if the actor length changes

Try eval as mentioned below to convert the string list to actual list

actor = ["[Emil Eifrem,Hugo Weaving,Laurence Fishburne]"]
actor = actor[0].strip('[]').split(',')
title = ["The Matrix"] * len(actor)

actors = pd.DataFrame(list(zip(actor, title)),
                      columns = ["actor", "title"])
# Output

      actor               title
0   Emil Eifrem         The Matrix
1   Hugo Weaving        The Matrix
2   Laurence Fishburne  The Matrix
Answered By: Abhi
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.