3

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

1
  • What is the type of df['C'] ? try type(df['C'][0]) . Commented Oct 7, 2015 at 4:47

3 Answers 3

4

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 .

Sign up to request clarification or add additional context in comments.

1 Comment

I will try that as well thanks. And keep in mind the advice.
1
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

1 Comment

Thanks, looks like working for me! This function is of great help to handle series of series
0
def is_in_list():
    for ele in df['C']:
    if ele in list:
        return True
    return False;

Maybe this function can make it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.