【问题标题】:Use Numpy “where” with multiple conditions but do nothing if condition fails在多个条件下使用 Numpy “where”,但如果条件失败则不执行任何操作
【发布时间】:2020-08-04 11:43:00
【问题描述】:

如果您能使用 Jupyter Notebook 帮助我完成这项任务,我将不胜感激。

1 store = vat['Sum of VAT'].notnull()
2 store1 = vat['Comment'] == "VAT"
3 vat['Comment'] = np.where(vat['External name'] == 'GBTICJE','Reverse charge + IC',vat['Comment'] )

我使用代码 3 根据“外部名称”列中的字符串值“GBTICJE”将字符串值提取到“评论”列,如果不满足条件,则不会覆盖“评论”列中的现有字符串。

但是如何将代码 1 和 2 集成到代码 3 中?这样我就可以在执行初始代码 3 之前先根据 1 和 2 中的两个条件过滤数据帧?

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python-3.x pandas numpy jupyter-notebook where-clause


    【解决方案1】:

    这是我的建议:

    vat.loc[(vat['Sum of VAT'].notnull()) &
            (vat['Comment'] == "VAT") &
            (vat['External name'] == 'GBTICJE'), "Comment"] = 'Reverse charge + IC'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2013-04-26
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多