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 用字典中的另一个值替换dataframe和中的值_Python_Dataframe - Fatal编程技术网

Python 用字典中的另一个值替换dataframe和中的值

Python 用字典中的另一个值替换dataframe和中的值,python,dataframe,Python,Dataframe,我正在构建一个单词计数向量,我的进度如下所述: 我以这种形式构建了熊猫数据框: 示例数据帧: file body 0 PP3169 {'Under':1, 'natur':6, 'view':10, 'condit':2, 'human':7,...} 我还有一本字典,里面有每个单词及其ID 具有单词ID的词典片段: {'AFOSR': '0', 'ARO': '1', 'AUC': '2', 'Accuracy': '3', 'Acknowledgments': '4

我正在构建一个单词计数向量,我的进度如下所述:

我以这种形式构建了熊猫数据框:

示例数据帧:

    file     body
0  PP3169   {'Under':1, 'natur':6, 'view':10, 'condit':2, 'human':7,...}
我还有一本字典,里面有每个单词及其ID

具有单词ID的词典片段:

{'AFOSR': '0', 'ARO': '1', 'AUC': '2', 'Accuracy': '3', 'Acknowledgments': '4', 'Active': '5', 'Adam': '6', 'Adaptive': '7', 'After': '8',...}
在上述词典中,每个单词都被分配了一个“单词ID”。例如,AFOSR的ID为0,ARO的ID为1,依此类推

目标:我想用单词ID字典中的相应值替换数据框中的字典键。假设在数据框中,单词“under”在单词ID字典中的ID为477,则数据框中的字符串将替换为其各自的ID。因此,它将为477:1,格式为

数据帧的预期输出格式:

    file     body
0  PP3169   {<word ID of word#1> : <frequency of word#1>, <word ID of word#2> : <frequency of word#2>, <word ID of word#3> : <frequency of word#3>,...}
文件体
0 PP3169{:,:,:,…}

非常感谢您对这个问题的帮助。

我想这就是您要寻找的代码:

(假设
wordID
表示每个单词及其ID的字典)

在替换过程中,需要使用
[0]
索引数据帧列以完成任务,因为它是数据帧中的一条记录。

我会尝试这种方法

new_body = {}
for i in body:
  new_body.update({ids[i] : body[i]})

body = new_body
new_body = {}
for i in body:
  new_body.update({ids[i] : body[i]})

body = new_body