I'm trying to tokenize the words within dataframe which looks like
A B C D E F
0 Orange robot x eyes discomfort striped tee nan
1 orange robot blue beams grin vietnam jacket nan
2 aquamarine robot 3d bored cigarette nan
After removing all the special characters the dataframe became a string like this
df_str = df.to_string(header=False)
import re
normalised_text = bayc_features_str.lower()
text = re.sub(r"[^\a-zA-Z0-9 ]","", normalised_text)
print(text)
1 orange robot x eyes discomfort striped tee nan
2 orange robot blue beams grin vietnam jacket nan
3 aquamarine robot 3d bored cigarette nan
so when I tokenize this string, with below code
def tokenize(obj):
if obj is None:
return None
elif isinstance(obj, str):
return word_tokenize(obj)
elif isinstance(obj, list):
return [tokenize(i) for i in obj
else:
return obj
tokenized_text = (tokenize(text))
I get the output
['orange', 'robot', 'x', 'eyes', 'discomfort', 'striped', 'tee', nan,'orange', 'robot', 'blue', 'beams', 'grin', 'vietnam', 'jacket', nan,'aquamarine', 'robot', '3d', 'bored', 'cigarette', nan, 'sea', 'captains', 'hat']
which is quite different from the output I expected
[['orange'], ['robot'], ['x', 'eyes'], ['discomfort'], ['striped', 'tee'], nan]
[['orange'], ['robot'], ['blue', 'beams'], ['grin'], ['vietnam', 'jacket'], nan]
[['aquamarine'], ['robot'], ['3d'], ['bored', 'cigarette'], nan, ['sea', 'captains', 'hat']]
Any ideas on how can I get the output I expected? Any help would be greatly appreciated!