Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional lambda apply across dataframe based on list equality

I have a dataframe df whos columns contain lists of strings

df =      A               B 
        ['-1']    , ['0','1','2']
     ['2','4','3'],     ['2']
      ['3','8']   ,    ['-1']

I want to get the length of all the lists except the ones that are ['-1'] for the lists that are ['-1'] I want them to be -1

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = A   B 
    -1,  3
     3,  1
     2, -1

I’ve tried

df.apply(lambda x: x.str.len() if not x == ['-1'] else -1)

and got the error ('Lengths must match to compare', (132,), (1,))

I have also tried

data_copy[colBeliefs] = data_copy[colBeliefs].apply(lambda x: x.str.len() if '-1' not in x else -1)

but this produces the wrong output where ['-1'] becomes 1 rather than -1

I’m not sure how I can apply functions to a dataframe based on the whether an entry in a dataframe is equal to a list, or whether an item is in a list.

EDIT: Output of df.head().to_dict()

{'A': {0: ['-1'],
       1: ['2','4','3'],
       2: ['3','8']},
 'B': {0: ['0','1','2'],
       1: ['2'],
       2: ['-1']}}

>Solution :

You could do:

df.applymap(lambda x: -1 if (ln:=len(x)) == 1 and x[0] == '-1' else ln)

   A  B
0 -1  3
1  3  1
2  2 -1

Edit:

If yousing python < 3.8 Use the following:

df.applymap(lambda x: -1 if len(x) == 1 and x[0] == '-1' else len(x))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading