我有一个数据框,我想用一个列中的值替换另一个数据帧中的值。
df = pd.DataFrame({'id1': [1001,1002,1001,1003,1004,1005,1002,1006],
'value1': ["a","b","c","d","e","f","g","h"],
'value3': ["yes","no","yes","no","no","no","yes","no"]})
dfReplace = pd.DataFrame({'id2': [1001,1002],
'value2': ["rep1","rep2"]})
我需要使用具有公共密钥的groupby,并且当前的解决方案是使用循环。是否有一种更优雅(更快)的方法来使用.map(apply)等。我想初始使用pd.update(),但似乎没有正确的方法。
groups = dfReplace.groupby(['id2'])
for key, group in groups:
df.loc[df['id1']==key,'value1']=group['value2'].values
输出
df
id1 value1 value3
0 1001 rep1 yes
1 1002 rep2 no
2 1001 rep1 yes
3 1003 d no
4 1004 e no
5 1005 f no
6 1002 rep2 yes
7 1006 h no
答案 0 :(得分:3)
尝试merge():
merge = df.merge(dfReplace, left_on='id1', right_on='id2', how='left')
print(merge)
merge.ix[(merge.id1 == merge.id2), 'value1'] = merge.value2
print(merge)
del merge['id2']
del merge['value2']
print(merge)
输出:
id1 value1 value3 id2 value2
0 1001 a yes 1001 rep1
1 1002 b no 1002 rep2
2 1001 c yes 1001 rep1
3 1003 d no NaN NaN
4 1004 e no NaN NaN
5 1005 f no NaN NaN
6 1002 g yes 1002 rep2
7 1006 h no NaN NaN
id1 value1 value3 id2 value2
0 1001 rep1 yes 1001 rep1
1 1002 rep2 no 1002 rep2
2 1001 rep1 yes 1001 rep1
3 1003 d no NaN NaN
4 1004 e no NaN NaN
5 1005 f no NaN NaN
6 1002 rep2 yes 1002 rep2
7 1006 h no NaN NaN
id1 value1 value3
0 1001 rep1 yes
1 1002 rep2 no
2 1001 rep1 yes
3 1003 d no
4 1004 e no
5 1005 f no
6 1002 rep2 yes
7 1006 h no
答案 1 :(得分:2)
如果你已经将索引设置为id,这会更清晰一些,但如果没有,你仍然可以在一行中完成:
>>> (dfReplace.set_index('id2').rename( columns = {'value2':'value1'} )
.combine_first(df.set_index('id1')))
value1 value3
1001 rep1 yes
1001 rep1 yes
1002 rep2 no
1002 rep2 yes
1003 d no
1004 e no
1005 f no
1006 h no
如果你分成三行并分别重命名和重新编制索引,你可以看到combine_first()
本身实际上非常简单:
>>> df = df.set_index('id1')
>>> dfReplace = dfReplace.set_index('id2').rename( columns={'value2':'value1'} )
>>> dfReplace.combine_first(df)