Tony Sherman
Tony Sherman

Reputation: 295

Python 2.7 replacing all values in a pandas DF

in a pandas dataframe: everything I try replaces the key,value pairs in the first column, but not in the first and second. Here is the code:

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)

And the output:

        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   

You can see I am not even trying to be terse, but I'm breaking it out to see if I can spot my error. Right now I'm stumped. TIA

Upvotes: 1

Views: 271

Answers (1)

jezrael
jezrael

Reputation: 863166

IIUC if need replace values in some column by dict simpliest is remove loop:

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

And if need map (where not match get NaN):

df['inResponseTo'] = df['inResponseTo'].map(module_names)
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           NaN   Web-Ducky  36931    NaN
19     Thank You    Depression       Ducky  36931      3

EDIT:

If need replace in multiple columns:

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

Upvotes: 1

Related Questions