简体   繁体   中英

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?

Try using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM