0

I am using apply function to process name1 column. I can pass in a fix value like 8 into the function but I wish to use the value (num_of_bit) from each row to process name1 column. When I use the code as below, I will get the error. I understand why I got this error but I do not know how to achieve what I want.

def signedProcessing(input,num_of_bit):
    if input < (2**num_of_bit)/2:
        input += 256
    return input

out_df.loc[out_df['range_type'] == "SIGNED", [name1]] = out_df[out_df['range_type']=="SIGNED"][name1].apply(signedProcessing,num_of_bit=out_df[out_df['range_type'] == "SIGNED"]['num_of_bit'])

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1
  • It looks like you should use pd.Series.where instead of applying. Commented Sep 12, 2020 at 6:49

1 Answer 1

1

If I understand the question correctly and you need to change the 'name1' value by the applying the function, where the 'range_type' is 'SIGNED', then you are not passing the 'num_of_bit' to apply, so it tries to use the whole column for each row. I suggest using a lambda function:

out_df.loc['name1'] = out_df[['name1', 'num_of_bit', 'range_type']].apply(lambda x: signedProcessing(x.name1, x.num_of_bits) if x.range_type=='SIGNED' else x.name1, axis=1)

This will change the 'name1' value on each row that has a 'SIGNED' range_type to the signedProcessing(value of name1, value of num_of_bits). If the range_type is something else, it won't change the name1 value.

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

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.