对于以下数据框df1:
sentence A 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
我需要从列句子创建一个字典,其中包含A,B,C和D列。
在下一步中,我需要将数据帧F2中的句子列映射到值A,B,C和D.输出如下:
sentences A 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
This is my code, but just for one column, I do not know how to do it for several columns:
equiv = df1.set_index (sentences)[A].to_dict()
df2[A]=df2[sentences].apply (lambda x:equiv.get(x, np.nan))
感谢。
答案 0 :(得分:0)
IIUC:
设定:
In [164]: df1
Out[164]:
sentence A 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
解决方案:
In [174]: df2[['sentences']].merge(df1[['sentence','A','B','C','D']],
left_on='sentences',
right_on='sentence',
how='outer')
Out[174]:
sentences sentence A 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