Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

python - 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 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...