简体   繁体   English

如何使用来自另一个数据帧(熊猫)的值更新空的 dataframe 值?

[英]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.仅当 dataframe1 中的行为空时,我才想用 dataframe2 中的等效值更新 dataframe1 中的空行。

Cases:案例:

数据框1

fig 1图。1

数据框2

fig 2图2

In the above example, I want to fill only empty rows of Price columns in dataframe1 with the equivalent Price columns from dataframe2.在上面的示例中,我只想用 dataframe2 中的等效价格列填充 dataframe1 中 Price 列的空行。

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

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

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