I have a pandas Series containing strictly non-negative integers like so:
1
2
3
4
5
I want to convert them into n-bits binary representation based on the largest value. For example, the largest value here is 5, so we would have 3 bits/3 columns, and the resulting series would be something like this
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
Thanks a lot in advance!
If your values are less than 255, you could unpackbits
:
s = pd.Series([1, 2, 3, 4, 5])
N = int(np.log2(s.max()))
powers = 2**np.arange(N, -1, -1)
out = pd.DataFrame(np.unpackbits(s.to_numpy(np.uint8)[:, None], axis=1)[:, -N-1:],
index=s.index, columns=powers)
If your have larger numbers, compute a mask with &
and an array of powers of 2:
s = pd.Series([1, 2, 3, 4, 5])
powers = 2**np.arange(int(np.log2(s.max())), -1, -1)
out = pd.DataFrame((s.to_numpy()[:, None] & powers).astype(bool).astype(int),
index=s.index, columns=powers)
Output:
4 2 1
0 0 0 1
1 0 1 0
2 0 1 1
3 1 0 0
4 1 0 1