for example, I have a df like this:
A
0 I like this
1 I like that
which was created by:
df = DataFrame({'A':['I like this','I like that']})
Now for df['A'], I want to convert this series to a list of lists, like this:
[['I like this'],['I like that']]
I searched about this but I only got suggestions like tolist(), which converts a series to a list, not what I look for
['I like this', 'I like that']
Can anyone help me with this? Thanks in advance! I use such df for gensim use.
CodePudding user response:
You need to apply
the list
operation on each row individually as well.
df.apply(list, axis=1).tolist()
If you just want this apply
on a few columns in your DF, simply slice into the DF using:
df[['A']].apply(list, axis=1).tolist()
Output
[['I like this'], ['I like that']]
CodePudding user response:
You can try list comprehension, to convert a list into a list of lists
import pandas as pd
df = pd.DataFrame({'A':['I like this','I like that']})
my_list = list(df['A'])
[[i] for i in my_list]
#Output: [['I like this'], ['I like that']]
CodePudding user response:
As an alternative:
new_list =list(map(lambda x:[x], list(df.A.values)))
#[['I like this'], ['I like that']]