Fill column of a dataframe from another dataframe

非 Y 不嫁゛ 提交于 2021-02-05 04:54:59

问题


I'm trying to fill a column of a dataframe from another dataframe based on conditions. Let's say my first dataframe is df1 and the second is named df2. df1 is described as bellow : +------+------+ | Col1 | Col2 | +------+------+ | A | 1 | | B | 2 | | C | 3 | | A | 1 | +------+------+ And : df2 is described as bellow : +------+------+ | Col1 | Col2 | +------+------+ | A | NaN | | B | NaN | | D | NaN | +------+------+ Each distinct value of Col1 has her an id number (In Col2), so what I want is to fill the NaN values in df2.Col2 where df2.Col1==df1.Col1 . So that my second dataframe will look like : df2 : +------+------+ | Col1 | Col2 | +------+------+ | A | 1 | | B | 2 | | D | NaN | +------+------+ I'm using Python 2.7


回答1:


Use drop_duplicates with set_index and combine_first:

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

If need check dupes only in id column:

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



回答2:


Here is a solution with the filter df1.Col1 == df2.Col1

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

It is even better to use loc (but less clear from my point of view)

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


来源:https://stackoverflow.com/questions/51097981/fill-column-of-a-dataframe-from-another-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!