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
313 views
in Technique[技术] by (71.8m points)

python - Use pandas to replace values in one df with corresponding values in another

This has been driving me crazy for a few hours. It feels like there is a simple solution that I'm close to but missing.

I have two DFs with two columns country_name and listed_country.

In df1 there are ~ 200 records and listed_country is either Yes or NaN.

In df2 there are ~12 records with listed_country == Yes for every record.

There are 3 records in df2 that I want to use to replace the corresponding NaN values in df1.

I've tried this a few different ways. First I narrowed down the delta between the two list to just take the country names I want to update. Then using list comparison with query and fillna. Even using inplace=True this runs but doesn't update df1.

eul = ['American Samoa', 'Guam', 'Virgin Islands, US'] 

df1.loc[:, ['country_name', 'listed_country']].query("country_name == @eul").fillna('Yes', inplace=True)

Then I tried pandas combine_first function, but it overwrites all NaN values in df1, which is not what I want:

df1.combine_first(df2)

Then I tried this solution proposed in another post, but it creates a new column, so again does not do what I want:

d = df2.set_index('country_name').listed_country

df1['listed_country'].replace(d, inplace=True)

Feels like this is a common use case and there must be a simple solution I'm overlooking?

Example dfs

df1:


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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...