首页 文章

从另一个数据框填充数据框的列

提问于
浏览
4

我说我的第一个数据帧是df1,第二个数据帧是df2 . df1 is described as bellow : +------+------+ | Col1 | Col2 | +------+------+ | A | 1 | | B | 2 | | C | 3 | | A | 1 | +------+------+ 并且: df2 is described as bellow : +------+------+ | Col1 | Col2 | +------+------+ | A | NaN | | B | NaN | | D | NaN | +------+------+ Col1的每个不同值都有一个id号(在Col2中),所以我想要的是填充df2.Col2中的NaN值,其中df2.Col1 == df1.Col1 . 所以我的第二个数据框将如下所示: df2 : +------+------+ | Col1 | Col2 | +------+------+ | A | 1 | | B | 2 | | D | NaN | +------+------+ 我正在使用Python 2.7

2 回答

  • 0

    drop_duplicatesset_indexcombine_first一起使用:

    df = df2.set_index('Col1').combine_first(df1.drop_duplicates().set_index('Col1')).reset_index()
    

    如果需要仅在 id 列中检查dupe:

    df = df2.set_index('Col1').combine_first(df1.drop_duplicates().set_index('Col1')).reset_index()
    
  • 0

    这是一个带过滤器的解决方案 df1.Col1 == df2.Col1

    df2['Col2'] = df1[df1.Col1 == df2.Col1]['Col2']
    

    使用 loc 更好(但从我的观点来看不太清楚)

    df2['Col2'] = df1.loc[df1.Col1 == df2.Col2, 'Col2']
    

相关问题