【问题标题】:if x['column 1] = y['column 1'] then replace that value with value in y['column 2'] in python or R如果 x['column 1] = y['column 1'] 然后用 python 或 R 中的 y['column 2'] 中的值替换该值
【发布时间】:2018-11-25 11:57:28
【问题描述】:

我有一个每天更新的大型数据集,但我从中获取数据的系统改变了一些值。我需要更正主数据中的这些值,使其与其他报告匹配。我确定了这些值并在另一列中创建了一个具有正确值的数据框。我只想用 Python 中的更正名称替换 50 个名称。

如果x['column 1] == y['column 1'] 则将该值替换为y['column 2'] 中的值

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:
    x.loc[x['column 1']==y['column 1'], 'column 1'] = y['column 2']
    

    【讨论】:

    • 'ValueError: Can only compare the same-labeled Series objects' 。我认为这是因为它们的长度不同。如果 x[column 1] 在 y[column 1] 中,你会怎么做,然后用 y[column 2] 替换
    【解决方案2】:

    使用np.where

    y['new_column'] = np.where(x['column 1'] == y['column 1'],y['column 2'],The condition that u want whenever the if is not sastify)
    

    【讨论】:

    • 'ValueError: Can only compare the same-labeled Series objects' 。我认为这是因为它们的长度不同。如果 x[column 1] 在 y[column 1] 中,你会怎么做,然后用 y[column 2] 替换
    猜你喜欢
    • 2021-12-21
    • 2019-03-23
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多