简体   繁体   中英

How to update the empty dataframe values with values from another dataframe(Pandas)?

I want to update empty rows in dataframe1 with the equivalent values from dataframe2 only if the rows in dataframe1 is empty.

Cases:

数据框1

fig 1

数据框2

fig 2

In the above example, I want to fill only empty rows of Price columns in dataframe1 with the equivalent Price columns from dataframe2.

Any ideas or suggestions for this?

import pandas as pd

df1 = pd.read_csv('dataframe1.csv')
df2 = pd.read_csv('dataframe2.csv')

Try this:

 df2dict = df2.set_index(['Product Code'])['Price'].squeeze().to_dict()
 # maps from df2['Product Code'] to empty columns in df
 df['Price'] = df['Price'].fillna(df['Product Code'].map(df2dict))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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