【问题标题】:How to update the empty dataframe values with values from another dataframe(Pandas)?如何使用来自另一个数据框(熊猫)的值更新空数据框值?
【发布时间】:2021-01-04 16:15:05
【问题描述】:

只有当 dataframe1 中的行为空时,我才想用 dataframe2 中的等效值更新 dataframe1 中的空行。

案例:

图1

图2

在上面的示例中,我只想用 dataframe2 中的等效价格列填充 dataframe1 中 Price 列的空行。

对此有何想法或建议?

import pandas as pd

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

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我看到这篇文章被编辑了,但不应该是df['Product Code'].map(df2dict)吗?这将返回一个与 df1 匹配的系列,但只会填写 nan 值。
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多