Python2.7替换文件中的所有值

Python2.7替换文件中的所有值,python,python-2.7,pandas,Python,Python 2.7,Pandas,在数据帧中:我尝试的所有内容都会替换第一列中的键、值对,但不会替换第一列和第二列中的键、值对。代码如下: module_names = {442990: 'Thank You', 442896:'Depression', 442924:'Irritability', 442879:'Anxiety', 442985:'sleep', 442875:'Meds'} for key, value in module_names.iteritems(): df['module'].replac

在数据帧中:我尝试的所有内容都会替换第一列中的键、值对,但不会替换第一列和第二列中的键、值对。代码如下:

module_names = {442990: 'Thank You', 442896:'Depression', 442924:'Irritability', 442879:'Anxiety', 442985:'sleep', 442875:'Meds'}
for key, value in module_names.iteritems():
    df['module'].replace(key,value,inplace=True)
print df.head(15)
for key, value in module_names.iteritems():
    df['inResponseTo'].replace(key,value,inplace=True)
print df.head(15)
以及输出:

        module inResponseTo botNickname  botID result  \
1      Thank You       442896   Web-Ducky  36931      4   
3     Depression       442924   Web-Ducky  36931      4   
5   Irritability       442879   Web-Ducky  36931      3   
7        Anxiety       442985   Web-Ducky  36931      4   
9          sleep       442875   Web-Ducky  36931    yes   
11          Meds       442875   Web-Ducky  36931    NaN   
13          Meds       442864   Web-Ducky  36931    NaN   
19     Thank You       442896       Ducky  36931      3   

你可以看到,我甚至不想简明扼要,但我正在打破它,看看我是否能发现我的错误。现在我被难住了。TIA

IIUC如果需要,用dict替换某些列中的值最简单的是删除循环:

df['inResponseTo'].replace(module_names,inplace=True)
print df
          module  inResponseTo botNickname  botID result
1      Thank You    Depression   Web-Ducky  36931      4
3     Depression  Irritability   Web-Ducky  36931      4
5   Irritability       Anxiety   Web-Ducky  36931      3
7        Anxiety         sleep   Web-Ducky  36931      4
9          sleep          Meds   Web-Ducky  36931    yes
11          Meds          Meds   Web-Ducky  36931    NaN
13          Meds        442864   Web-Ducky  36931    NaN
19     Thank You    Depression       Ducky  36931      3
如果需要(其中不匹配get
NaN
):

编辑:

如果需要多栏:

print (df)
          module  inResponseTo botNickname  botID  result
1      Thank You        442896   Web-Ducky  36931  442896
3     Depression        442924   Web-Ducky  36931  442924
5   Irritability        442879   Web-Ducky  36931  442879
7        Anxiety        442985   Web-Ducky  36931  442985
9          sleep        442875   Web-Ducky  36931  442875
11          Meds        442875   Web-Ducky  36931  442875
13          Meds        442864   Web-Ducky  36931  442864
19     Thank You        442896       Ducky  36931  442896

df[['inResponseTo','result']] = df[['inResponseTo','result']].replace(module_names)
print (df)
          module  inResponseTo botNickname  botID        result
1      Thank You    Depression   Web-Ducky  36931    Depression
3     Depression  Irritability   Web-Ducky  36931  Irritability
5   Irritability       Anxiety   Web-Ducky  36931       Anxiety
7        Anxiety         sleep   Web-Ducky  36931         sleep
9          sleep          Meds   Web-Ducky  36931          Meds
11          Meds          Meds   Web-Ducky  36931          Meds
13          Meds        442864   Web-Ducky  36931        442864
19     Thank You    Depression       Ducky  36931    Depression

对不起,我仍然得到相同的结果。没有错误,只是更改了第一列。顺便说一句,尝试粘贴我的代码,但它不起作用,对不起。如果我将其应用于第一列,您的代码可以工作,但任何更改第二列的尝试都会使我受阻。顺便说一句,刚刚删除了“模块”列,但它仍然不起作用,第二列中是否有关键字:“inResponseTo”?嗨,尝试了新代码:df[['inResponseTo','module']=df['inResponseTo','module']]。替换(模块名称)仅替换模块列,而不是'inResponseTo'列。可能存在不同的数据类型,如字符串而不是int。因此需要
df['inResponseTo']=df['inResponseTo']。aType(int)
首先转换为int。
print (df)
          module  inResponseTo botNickname  botID  result
1      Thank You        442896   Web-Ducky  36931  442896
3     Depression        442924   Web-Ducky  36931  442924
5   Irritability        442879   Web-Ducky  36931  442879
7        Anxiety        442985   Web-Ducky  36931  442985
9          sleep        442875   Web-Ducky  36931  442875
11          Meds        442875   Web-Ducky  36931  442875
13          Meds        442864   Web-Ducky  36931  442864
19     Thank You        442896       Ducky  36931  442896

df[['inResponseTo','result']] = df[['inResponseTo','result']].replace(module_names)
print (df)
          module  inResponseTo botNickname  botID        result
1      Thank You    Depression   Web-Ducky  36931    Depression
3     Depression  Irritability   Web-Ducky  36931  Irritability
5   Irritability       Anxiety   Web-Ducky  36931       Anxiety
7        Anxiety         sleep   Web-Ducky  36931         sleep
9          sleep          Meds   Web-Ducky  36931          Meds
11          Meds          Meds   Web-Ducky  36931          Meds
13          Meds        442864   Web-Ducky  36931        442864
19     Thank You    Depression       Ducky  36931    Depression