Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract values from a column value containing a list of values

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading