Ishan Ishan - 1 year ago 56
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 :


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. -

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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download