首页 > 解决方案 > Check if values of a column is in another column array in a pandas dataframe

问题描述

I have the following dataframe:

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

I have 3 columns, where the column "Responsibility Type" hold string values and "Roles" is a list (or an array, any would work) with multiple values.

I want to check, row by row, if the value on the column "Responsibility Type" is in list of the "Roles" column.

Any idea how could I do that?

标签: pythonarrayspandaslistnumpy

解决方案


Try using:

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

推荐阅读