根据其他列中的非空值定义新列

问题描述 投票:0回答:1

我使用两个表进行外部连接。下面是表格。

我想创建一个名为 Job Number 的列,它查看 Job Number Salesforce 和 Job Number Coins 列并返回不为空的列。

我尝试使用以下代码:

if outer["Job Number_salesforce"] is not None:
    outer["Job Number"] = outer["Job Number_salesforce"]
else:
    outer["Job Number"] = outer["Job Number_coins"]

但是,正如您在作业编号列中看到的那样,如果第一列不为空,它会返回作业编号,但当第一列为空时,它不会返回第二列的值。

python pandas numpy join calculated-columns
1个回答
0
投票

这是解决问题的一种方法:

import numpy as np
import pandas as pd

# Create some sample DataFrame with the two columns that our code will use.
outer = pd.DataFrame(
    [
        ['11D01', '11D01'],
        ['11D01', '11D01'],
        ['11D01', None],
        ['31D01', '31D01'],
        ['31D01', '31D01'],
        [None, '31D01'],
        [None, '22D05'],
    ], columns=["Job Number_salesforce", "Job Number_coins"]
)

# np.where() works by identifying the rows where the condition (first
# parameter that was passed to it) is True or not.
# If the condition is True, then the value of the
# second parameter (values from the column "Job Number_salesforce") is used,
# otherwise, the value from the third parameter (values from the column
# "Job Number_coins") is used.
outer["Job Number"] = np.where(outer["Job Number_salesforce"].notnull(),
                               outer["Job Number_salesforce"],
                               outer["Job Number_coins"])

print(outer)
# Prints:
#
#   Job Number_salesforce Job Number_coins Job Number
# 0                 11D01            11D01      11D01
# 1                 11D01            11D01      11D01
# 2                 11D01             None      11D01
# 3                 31D01            31D01      31D01
# 4                 31D01            31D01      31D01
# 5                  None            31D01      31D01
# 6                  None            22D05      22D05

注意

上面的实现有两个潜在的氪石:

  1. 如果两列都有非缺失值,则来自 “职位编号_salesforce”列将被优先考虑。
  2. 如果两列都有缺失值,那么新创建的“工作编号”列仍然会包含缺失值。
© www.soinside.com 2019 - 2024. All rights reserved.