Add new Pandas Column based on whether list items in a column are inside a list

dendoniseden

I have a "list_of_sendlists" with sample items like this:

list_of_sendlistsA = ["VIP","Officials","2021Customers","2020Customers"]

and I have a dataframe that contains some email addresses, and the "send lists" that are assigned to them: (Note: for d and e below, there are values in the lists that are not in "list_of_sendlists"

email   listsB
[email protected] VIP,Officials
[email protected] Officials
[email protected] 
[email protected] Non-factor
[email protected] Officials,Resigned

The tasks I want to manage is to add a column "on_list", if the email address has a value inside the "listB" that matches with "list_of_sendlistsA"

email   listsB              on_list
[email protected] VIP,Officials       Yes
[email protected] Officials           Yes
[email protected]                     No
[email protected] Non-factor          No
[email protected] Officials,Resigned  Yes

I tried df.listsB = df.listB.str.split(',') to turn the "listB" column into a Python List,

and then df['onlist'] = df.apply(lambda k: any(x in df['sendlist'] for x in k), axis = 1)

in order to return a "True" value if there are intersections between the two lists for each email address, but I couldn't get what I want (all returned value of df['onlist'] were false)

May I know if someone could help me?

Thanks so much!

jezrael

Use set.disjoint for False if disjoint values, so possible pass to numpy.where:

list_of_sendlistsA = ["VIP","Officials","2021Customers","2020Customers"]
mask = df.listsB.fillna('').str.split(',').map(set(list_of_sendlistsA).isdisjoint)
df['onlist'] = np.where(mask,'No','Yes' )

print (df)
     email              listsB onlist
0  [email protected]       VIP,Officials    Yes
1  [email protected]           Officials    Yes
2  [email protected]                 NaN     No
3  [email protected]          Non-factor     No
4  [email protected]  Officials,Resigned    Yes

Your solution should be change:

df['listsB'] = df.listsB.str.split(',')
mask = df['listsB'].fillna('').apply(lambda k: any(x in list_of_sendlistsA for x in k))
df['onlist'] = np.where(mask, 'Yes', 'No')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

pandas mapping list to dict items for new column

How to add list items to a new dataframe column inside another list in r?

Pandas new column based on list of indexes

How to Add a Column Based on a Boolean List in Pandas?

Return a list of column names as new column based on a condition in pandas

Replace items in column based on list

How to add a column based on a comparison between an existing column and a list in pandas

New pandas column based on whether functions are called

Filter Items in a List in a Pandas Column

Create pandas duplicate rows based on the number of items in a list type column

Splitting dictionary/list inside a Pandas Column and convert as new dataframe

how to add list as a new column?

Add new column based on a list and sort date by newest

add a new column in pyspark dataframe based on matching values from a list

Add a new column to a numpy array with values based on list of indices

Add a new column to a dataframe with multiple condition based on list and a dataframe

Add elements from list to new data frame column based on condition

Add key values pair inside list into pandas dataframe column

How Do I Create New Pandas Column Based On Word In A List

Pandas dataframe create new column based on list of tuples

How to assign new column based on the list of string values in pandas

Pandas Implode and Create 2 New Column based on list value

Pandas - combine column values into a list in a new column

Add constant list to pandas column

Pandas add column using a list

How to make these list items fit inside the column

Add ID found in list to new column in pandas dataframe

How to add new element to pandas.DataFrame column which is list?

Add a static list to a new Pandas Dataframe column via apply

TOP Ranking

HotTag

Archive