我正在使用Python,我在数据框中有一个字符串列,看起来像这样:
df['set']
0 [911,3040]
1 [130055, 99832, 62131]
2 [19397, 3987, 5330, 14781]
3 [76514, 70178, 70301, 76545]
4 [79185, 38367, 131155, 79433]
我希望它是:
['911','3040'],['130055','99832','62131'],['19397','3987','5330','14781'],['76514',70178','70301','76545'],['79185','38367','131155','79433']
为了能够运行Word2Vec:
model = gensim.models.Word2Vec(df['set'] , size=100)
谢谢!
答案 0 :(得分:1)
如果您有一列字符串,我建议您以不同的方式查找here。
以下是我使用ast.literal_eval
的方式。
>>> import ast
>>> [list(map(str, x)) for x in df['set'].apply(ast.literal_eval)]
或者,使用pd.eval
-
>>> [list(map(str, x)) for x in df['set'].apply(pd.eval)] # 100 rows or less
或者,使用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']
]
答案 1 :(得分:0)
我认为你需要:
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']]
答案 2 :(得分:0)
创建新列(str_set
),并将set
列中的项目转换为字符串:
df["str_set"] = [[str(item) for item in df.loc[row, "set"]] for row in range(len(df["set"]))]
答案 3 :(得分:0)
使用简单的列表解析将每个元素转换为字符串并覆盖旧列:
df['set'] = [[str(i) for i in row] for row in df['set']]
根据提供的数据执行:
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']]
不确定这是否是大数据集的最快方式,因为有很多迭代。