简体   繁体   English

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

[英]Fill column of a dataframe from another dataframe

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,第二个被命名为 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 . Col1 的每个不同值都有一个 id 号(在 Col2 中),所以我想要的是填充 df2.Col2 中的 NaN 值,其中 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我正在使用 Python 2.7

Use drop_duplicates with set_index and combine_first :drop_duplicatesset_indexcombine_first

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

If need check dupes only in id column:如果只需要在id列中检查欺骗:

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

Here is a solution with the filter df1.Col1 == df2.Col1这是过滤器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)使用loc更好(但从我的角度来看不太清楚)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM