0

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:

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']}}
2
  • please include the output of df.head().to_dict() in your question Commented Apr 25, 2022 at 17:10
  • @onyambu I have included the output of df.head().to_dict(), or did you want the output of that after data_copy[colBeliefs] = data_copy[colBeliefs].apply(lambda x: x.str.len() if '-1' not in x else -1) Commented Apr 25, 2022 at 17:16

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting df.applymap(lambda x: -1 if (ln:=len(x)) == 1 and x[0] == '-1' else ln) SyntaxError: invalid syntax pointing to ln:=len(x)
@SemicolonExpected The syntax maybe because of the version you are using. do df.applymap(lambda x: -1 if len(x) == 1 and x[0] == '-1' else len(x))

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.