Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python字典操纵到数据框架中_Python_Pandas_Data Cleaning - Fatal编程技术网

将Python字典操纵到数据框架中

将Python字典操纵到数据框架中,python,pandas,data-cleaning,Python,Pandas,Data Cleaning,我有一个来自gensim word2vec包的word vector对象,可以使用model.wv.vocab访问“username”,使用model.wv[w]访问vectors 这是我正在使用的示例 for w in sample: print("ID:", w) print("Vector subset: \n", model.wv[w][:10]) ID: 1843 Vector subset: [ 0.08228672 -0.32398582 -0.1602492

我有一个来自gensim word2vec包的word vector对象,可以使用model.wv.vocab访问“username”,使用model.wv[w]访问vectors

这是我正在使用的示例

for w in sample:
    print("ID:", w)
    print("Vector subset: \n", model.wv[w][:10])

ID: 1843
Vector subset: 
 [ 0.08228672 -0.32398582 -0.16024925  0.44939137 -0.28749713  0.25965428
 -0.18141621  0.06290377  0.1270649   0.40421844]
ID: 866
Vector subset: 
 [-0.21120088  0.10489845  0.17965898  0.18383555 -0.24510185 -0.00716993
 -0.18718664  0.3398481   0.07536748 -0.5193063 ]
ID: 2819
Vector subset: 
 [ 0.33056906  0.20122662  0.0239714   0.1846028  -0.1632814  -0.4005747
 -0.02339112  0.22077617  0.20608544 -0.12747312]
ID: 4091
Vector subset: 
 [ 0.5139592   0.1325652  -0.19846869  0.02061795 -0.72117347 -0.5065503
 -0.2806759   0.13045706  0.5880965  -0.497771  ]
ID: 4871
Vector subset: 
 [-0.30731577  0.10253543  0.01026379  0.24779265  0.3701798  -0.16493073
  0.07395677 -0.4943776   0.02144529 -0.12544158]
ID: 6557
Vector subset: 
 [-0.01380698  0.03429209  0.11136885  0.10298727 -0.09034968 -0.09744099
  0.04731373  0.12851992  0.5266305  -0.14707205]
ID: 4691
Vector subset: 
 [-0.12838683  0.34491533  0.10016204 -0.00582217 -0.1514073   0.13864768
  0.05341618 -0.15653287  0.37432986  0.09268643]
ID: 409
Vector subset: 
 [ 0.01493216  0.06893755  0.10319904 -0.08454162 -0.08191169 -0.16257484
 -0.10028194 -0.02943738  0.3722616  -0.27091444]
ID: 8229
Vector subset: 
 [-0.72491664  0.28790048  0.04535258  0.57867676 -0.09895556 -0.01902669
 -0.03930351  0.551734   -0.2825539   0.1426454 ]
ID: 5222
Vector subset: 
 [-0.05142907 -0.3080357  -0.00205866 -0.02018788 -0.07856932 -0.46743438
 -0.29095295  0.44115666  0.34238762  0.2151215 ]
我需要将这些信息处理成一个类似于下面的数据框的表单,以便传递到脚本中:

    username        1       2       3       4       5   6
          00    0.023   0.232   -0.13   0.2424  -0.242  -0.22
          01    0.001   0.013   -0.232  0.3232  0.2324  -0.023234
          02    0.244   -0.24   -0.3555 0.444   -0.22   -0.2342
          03    0.5333  -0.99   -0.9242 -0.43   0.242   0.423
我目前的想法是创建一个用户名和转置向量字典,然后从字典中创建一个数据帧

vect_dict = {}
for w in model.wv.vocab:
    reshaped_vec = np.reshape(model.wv[w], (300, 1)).T
    vect_dict[w] = reshaped_vec
但是,这不会给我一个单独的用户名列和作为转置向量的行,每列都是向量的第i个索引

如何将给定的数据转换成此表单


谢谢大家!

您可以转置数据帧,这可能会使这更简单。我忘了model.wv是否支持将其简单地视为字典,但即使不支持,以下内容也会起作用:

vect_dict = {w: model.wv[w] for w in model.wv.vocab}
dataframe = pd.DataFrame(vect_dict).T
如下所示:

In [1]: pd.DataFrame({'a': [1,2,3], 'b': [2,3,4]}).T
Out[1]:
   0  1  2
a  1  2  3
b  2  3  4

无法复制,无法访问vocab。已更新以包含示例!Try:pd.DataFrame.from_记录{w:model.wv[w]for model.wv.vocab}中的w,orient='index'