Reputation: 145
I'm working in Python and I have a column in data frame that is a string and looks like that :
df['set']
0 [911,3040]
1 [130055, 99832, 62131]
2 [19397, 3987, 5330, 14781]
3 [76514, 70178, 70301, 76545]
4 [79185, 38367, 131155, 79433]
I would like it to be:
['911','3040'],['130055','99832','62131'],['19397','3987','5330','14781'],['76514',70178','70301','76545'],['79185','38367','131155','79433']
in order to be able to run Word2Vec:
model = gensim.models.Word2Vec(df['set'] , size=100)
Thanks !
Upvotes: 1
Views: 964
Reputation: 639
Convert every single element to a string with a simple list comprehension and overwrite your old column:
df['set'] = [[str(i) for i in row] for row in df['set']]
Executed on the data provided:
data_col = [911,3040], [130055, 99832, 62131], [19397, 3987, 5330, 14781], [76514, 70178, 70301, 76545],[79185, 38367, 131155, 79433]
out = [[str(i) for i in row] for row in data_col]
out
[['911', '3040'],
['130055', '99832', '62131'],
['19397', '3987', '5330', '14781'],
['76514', '70178', '70301', '76545'],
['79185', '38367', '131155', '79433']]
Not sure if this is the fastest way for a big data set as there are a lot of iterations.
Upvotes: 0
Reputation: 4660
To create a new column (str_set
) with the items in the set
column converted to string:
df["str_set"] = [[str(item) for item in df.loc[row, "set"]] for row in range(len(df["set"]))]
Upvotes: 0
Reputation: 402483
If you have a column of strings, I'd recommend looking here at different ways of parsing it.
Here's how I'd do it, using ast.literal_eval
.
>>> import ast
>>> [list(map(str, x)) for x in df['set'].apply(ast.literal_eval)]
Or, using pd.eval
-
>>> [list(map(str, x)) for x in df['set'].apply(pd.eval)] # 100 rows or less
Or, using yaml.load
-
>>> import yaml
>>> [list(map(str, x)) for x in df['set'].apply(yaml.load)]
[
['911', '3040'],
['130055', '99832', '62131'],
['19397', '3987', '5330', '14781'],
['76514', '70178', '70301', '76545'],
['79185', '38367', '131155', '79433']
]
Upvotes: 1
Reputation: 862611
I think you need:
model = gensim.models.Word2Vec([[str(y) for y in x] for x in df['set']] , size=100)
L = [[str(y) for y in x] for x in df['set']]
print (L)
[['911', '3040'],
['130055', '99832', '62131'],
['19397', '3987', '5330', '14781'],
['76514', '70178', '70301', '76545'],
['79185', '38367', '131155', '79433']]
Upvotes: 0