Ishan Ishan - 3 months ago 8
Python Question

accessing the tokenized words in python

Suppose we have values in query column of a panda data frame which are tokenized using the split() function like

query[4] = "['rain', 'shower', 'head']".


Now I want to perform some operations on individual words. So, I converted it into list and iterated through it using for loop like like :

l=list(query[4])

for word in l : word=func(word)


But it is storing each alphabets on the list like -
['[', "'", 'r', 'a', 'i', 'n', "'", ','
and so on.

I have even tried to use join function i.e. -
''.join(word)
and
''.join(l)


But still nothing is working for me.
Can you suggest something here. Any help will be appreciated.

Answer

You need to convert to string to actual list:

data = eval(query[4])

Then loop through the data:

for word in data: word = func(word)