I am new to python and learning to use dataframes and list comprehensions. I have the following dataframe:
df1=pd.DataFrame({'names':[[['Hans Peter'],['Harry Potter']],[['bla bla'],['some string']]]})
now i want to split each sublist into words. For a single list of lists i could use
x=[['Hans Peter'],['Harry Potter'],['bla bla'],['some string here']]
res=[]
for list in x:
res.append(str(list[0]).split())
but how can i iterate this over a dataframe? i think i have to build a list comprehensions and then use the apply()
method to overgo the .append
? but i dont know how to do this.
I would build the list comprehension for a single list like this:
res = [str(list[0]).split for list in x]
but i get a list containing this functions:
[<function str.split(sep=None, maxsplit=-1)>,...]
the expected output for a DataFrame would be
0 [['Hans','Peter],['Harry','Potter']]
1 [['bla','bla'],['some','string']]