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

How do you generate a List of Lists from Long Strings in a DataFrame Column?

Have a very awkward setup. I have a DataFrame with long strings in the second column, which I want to break up and convert into a list of lists.

   col1  col2      
0  P     mary jane clare     
1  Q     tom dick harry     
2  R     sam jack bill    

I’d like the following list of lists as output:

[["mary", "jane", "clare"],["tom", "dick", "harry"],["sam", "jack", "bill"]]

Very unsure how to approach this one.

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

Thanks a million.

>Solution :

Split col2 using white space and then apply the to_list() method to come up with a single list

df['col2'].str.split('\s').to_list()
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