Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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_R_List_Dictionary_Keyword - Fatal编程技术网

Python 如何将带标签的关键字行和权重分解为关键字列

Python 如何将带标签的关键字行和权重分解为关键字列,python,r,list,dictionary,keyword,Python,R,List,Dictionary,Keyword,我正在寻找一种在r或python中获取字典的方法,从excel工作簿中获取字典数组,并对其进行转换,使每一列都是关键字,每一行都具有相关的权重。注意:每个单元格都有内容填充关键字和相关权重,格式为“name::abc;weight::2.0;name::def;weight::40.03” [输入] ID | keywords 1 | (name : abc; weight : 2.0) 2 | (name : def; weight : 3.1; name : abc; we

我正在寻找一种在r或python中获取字典的方法,从excel工作簿中获取字典数组,并对其进行转换,使每一列都是关键字,每一行都具有相关的权重。注意:每个单元格都有内容填充关键字和相关权重,格式为“name::abc;weight::2.0;name::def;weight::40.03”

[输入]

 ID | keywords  
 1  | (name : abc; weight : 2.0)  
 2  | (name : def; weight : 3.1; name : abc; weight : 32.1)  
 3  | (name : ghi; weight 3.0)
[输出]

 ID | abc | def | ghi   
 1  | 2.0 | 0   | 0  
 2  | 32.1| 3.1 | 0  
 3  | 0   | 0   | 3.0  
因此,这其中的关键部分是,每个关键字前面都有“name::”和后面的“;weight::”表示权重,尽管每个关键字的字符数或用于权重的数字没有设置限制。而且关键字的顺序也不尽相同

我曾尝试将字符串转换为字典,然后尝试使用Joris的fix(from)转换字典数组,但没有效果

我还尝试重新格式化源文件以删除名称和权重标签

df_dict = df.to_dict()

df[:,1].apply(pd.Series)
但是,这将返回typeError:“(slice(None,None,None),1)”是无效的键

我开始相信,如果我有一列字典要转换,而不是上面提到的解决方案中的一行,那么这个解决方案就行不通了