Reputation: 393
I have a Dataframe that look like this (small version):
A B C
0 125 ADB [AF:12]
1 189 ACB [AF:78, AF:85, AF:98]
2 148 ADB []
3 789 ARF [AF:89, AF:85, AF:12]
4 789 BCD [AF:76, AF:25]
How can I see if some of the items in column "C" are in a list?
knowing that when I do type(df.C) I get class 'pandas.core.series.Series'
if for example the list is:
['AF:12', 'AF25']
The expected output would be:
A B C D
0 125 ADB [AF:12] True
1 189 ACB [AF:78, AF:85, AF:98] False
2 148 ADB [] False
3 789 ARF [AF:89, AF:85, AF:12] True
4 789 BCD [AF:76, AF:25] True
I have tried df['D'] = df['C'].isin(list)
but get False everywhere probably because "C" is a list of list.
Is there a way to get around that?
Any help would be greatly appreciated
Upvotes: 3
Views: 881
Reputation: 91007
If the type of elements of C
column is list
, then I believe one method to do this would be to use set
intersection between your list and the elements of C
column using Series.apply
method . Example -
setlst = set(yourlist)
df['D'] = df['C'].apply(lambda x: bool(setlst.intersection(x)))
You can confirm that C
is of type list, if type(df['C'][0])
is list
.
Also please note using list as a variable name is not recommended as it would shadow the built in type list
.
Upvotes: 4
Reputation: 7832
data = {'B':['ADB','ACB','ADB','ARF','BCD'],
'A':[125,189,148,789,789],
'C':[['AF:12'],['AF:78', 'AF:85', 'AF:98'],[],
['AF:89', 'AF:85', 'AF:12'],['AF:76', 'AF:25']]}
df = pd.DataFrame(data)
def in_list(list_to_search,terms_to_search):
results = [item for item in list_to_search if item in terms_to_search]
if len(results) > 0:
return 'True'
else:
return 'False'
df['D'] = df['C'].apply(lambda x: in_list(x, ['AF:12', 'AF:25']))
Result:
A B C D
0 125 ADB [AF:12] True
1 189 ACB [AF:78, AF:85, AF:98] False
2 148 ADB [] False
3 789 ARF [AF:89, AF:85, AF:12] True
4 789 BCD [AF:76, AF:25] True
Upvotes: 1
Reputation: 8578
def is_in_list():
for ele in df['C']:
if ele in list:
return True
return False;
Maybe this function can make it.
Upvotes: 0