2

I have a boolean pandas DataFrame, as follow

aaa = pd.DataFrame([[False,False,False], [True,True,True]])

I want to convert it to a binary number array, for this DataFrame "aaa", the result is [000,111]

How can I implement this conversion?

Any help will be greatly appreciated. Thanks

4 Answers 4

5

You can do:

aaa = pd.DataFrame([[False,False,False],
                      [True,True,True]])
aaa=aaa.astype(int)

Then aaa is

    0   1   2
0   0   0   0
1   1   1   1

If you want to get the array ['000','111'] you can do:

aaa = pd.DataFrame([[False,False,False],
                      [True,True,True]])
aaa=aaa.astype(int).astype(str)
[''.join(i) for i in aaa.values.tolist()]
Sign up to request clarification or add additional context in comments.

Comments

1

You can convert after int and str to numpy array by values and then sum:

print (aaa.astype(int).astype(str).values.sum(axis=1))
['000' '111']

Comments

1

You can multiply by a bit shifted operator to simulate powers of two, sum, then convert to binary

aaa.mul(np.arange(3)[::-1] << 1).sum(1).apply(bin)

0      0b0
1    0b110
dtype: object

Notice how np.arange(3)[::-1] << 1 is successive powers of 2

array([4, 2, 0])

You can take this further by manipulating with str operations

aaa.mul(
    np.arange(3)[::-1] << 1
).sum(1).apply(bin).str.replace('0b', '').str.zfill(3)

0    000
1    110
dtype: object

Comments

0

I would do one of the following:

a.astype(int).astype(str).sum(axis=1).astype(int).astype(str)

but this is a bit too much retyping to my taste.

Another possibility is to use apply:

a.astype(int).astype(str).apply(lambda x: ''.join(list(x)))

But what seems cleanest to me is to obtain the desired number by multiplication, then convert it to binary:

a.dot([4, 2, 1]).map(lambda x: bin(x))

of course, if you don't want the '0b' on the beginning, you just use

a.dot([4, 2, 1]).map(lambda x: bin(x)[2:])

1 Comment

Multiplication is a great idea!

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.