python - creating dictionary from multiple columns in pandas -


for following data frame df1:

sentence               b     c      d      f       g  dizzy           1       1     0      0      k       1  head            0       0     1      0      l       1  nausea          0       0     0      1      fd      1  zap             1       0     1      0      g       1  dizziness       0       0     0      1      v       1      

i need create dictionary column sentence columns a, b, c,and d.

in next step, need map sentences column in data frame f2 value a, b, c, , d. output this:

   sentences               b     c      d                  dizzy           1       1     0      0      happy                   head            0       0     1      0                   nausea          0       0     0      1      fill out                 zap             1       0     1      0                   dizziness       0       0     0      1         code, 1 column, not know how several columns:   equiv = df1.set_index (sentences)[a].to_dict() df2[a]=df2[sentences].apply (lambda x:equiv.get(x, np.nan)) 

thanks.

iiuc:

setup:

in [164]: df1 out[164]:     sentence   b  c  d   f  g 0      dizzy  1  1  0  0   k  1 1       head  0  0  1  0   l  1 2     nausea  0  0  0  1  fd  1 3        zap  1  0  1  0   g  1 4  dizziness  0  0  0  1   v  1  in [165]: df2 out[165]:    sentences 0      dizzy 1      happy 2       head 3     nausea 4   fill out 5        zap 6  dizziness 

solution:

in [174]: df2[['sentences']].merge(df1[['sentence','a','b','c','d']],                                     left_on='sentences',                                    right_on='sentence',                                     how='outer') out[174]:    sentences   sentence       b    c    d 0      dizzy      dizzy  1.0  1.0  0.0  0.0 1      happy        nan  nan  nan  nan  nan 2       head       head  0.0  0.0  1.0  0.0 3     nausea     nausea  0.0  0.0  0.0  1.0 4   fill out        nan  nan  nan  nan  nan 5        zap        zap  1.0  0.0  1.0  0.0 6  dizziness  dizziness  0.0  0.0  0.0  1.0 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -