1

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 !

James
  • 32,991
  • 4
  • 47
  • 70
Adi Milrad
  • 135
  • 3
  • 9

4 Answers4

1

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']
 ]
cs95
  • 379,657
  • 97
  • 704
  • 746
0

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']]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

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"]))]
Toby Petty
  • 4,431
  • 1
  • 17
  • 29
0

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.

Steven
  • 619
  • 1
  • 7
  • 21