1

I have a Dataframe with two columns, PORT1 and PORT2, I am trying to convert each column to binary value and calculating a new value with some rules.

Example:

PORT1 = 16
PORT2=1
  1. first calculating binary value for PORT1+1=17 and PORT2-1=0, I must have that with len og eight integer : 00010001 and 00000000
  2. Then converting to integer this number : 01000010001000000000000000000000

My final column will be 1109393408

I tried to do this but it saying that it can't interpret a series like an integer.

DF['new_column'] = int('010'+bin(DF['PORT1']+1)[2:].zfill(8)+'0000'+bin(DF['PORT2']-1)[2:].zfill(8)+'000000000',2)

1 Answer 1

1

Your solution is ok but you need to use apply function:

DF['new_column'] = DF.apply(lambda row: int('010'+bin(row['PORT1']+1)[2:].zfill(8)+'0000'+bin(row['PORT2']-1)[2:].zfill(8)+'000000000',2), axis= 1)

Output:

   PORT1    PORT2   new_column
0   16      1       1109393408
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.