Home > Blockchain >  Extract values from a column value containing a list of values
Extract values from a column value containing a list of values

Time:10-16

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"])

CodePudding user response:

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

CodePudding user response:

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
  • Related