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

Time:04-28

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.

Thanks a million.

CodePudding user response:

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