【问题标题】:Check if values of a column is in another column array in a pandas dataframe检查一列的值是否在熊猫数据框中的另一个列数组中
【发布时间】:2020-05-08 10:36:45
【问题描述】:

我有以下数据框:

Person Number   Responsibility Type Roles
0   10000170    DSC HR Business Partner [DSC Employee Custom, DSC HR Business Partner,...
1   10000479    DSC HR Business Partner [DSC Employee Custom, DSC HR Business Partner,...
2   10001347    DSC HR Business Partner [DSC HR Business Partner, DSC HR Business Part...
3   10001754    DSC HR Business Partner Approver    [DSC Line Manager, DSC Employee Custom, DSC He...
4   10001754    DSC Head of HR  [DSC Line Manager, DSC Employee Custom, DSC He...

我有 3 列,其中“职责类型”列保存字符串值,“角色”是一个包含多个值的列表(或数组,任何都可以)。

我想逐行检查“职责类型”列上的值是否在“角色”列的列表中。

知道我该怎么做吗?

【问题讨论】:

    标签: python arrays pandas list numpy


    【解决方案1】:

    尝试使用:

    df['col'] = df.apply(lambda x: x['Responsibility Type'] in x['Roles'], axis=1)
    print(df)
    

    【讨论】:

    • 它不起作用。我需要它逐行返回一个布尔值,如果是真或假
    • @PauloCortez 再试一次,我编辑了它,如果有效请接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2021-07-15
    • 2021-09-02
    相关资源
    最近更新 更多