# Convert Number To Binary And Store In Multiple Columns In Pandas Using Python

## 06 February 2019 - 1 answer

I want to convert a number to binary and store in multiple columns in Pandas using Python. Here is an example.

``````df = pd.DataFrame([['a', 1], ['b', 2], ['c', 0]], columns=["Col_A", "Col_B"])

for i in range(0,len(df)):
df.loc[i,'Col_C'],df.loc[i,'Col_D'] = list( (bin(df.loc[i,'Col_B']).zfill(2) ) )
``````

I am trying to convert a binary and store it in a multiple columns in dataframe. After converting number to Binary, output has to contains 2 digits. It is working fine.

Question: If my dataset contains thousands of records, I can see performance difference. If I want to improve performance of above code how do we do it? I tried using following single line code, which didn't work for me.

``````df[['Col_C','Col_D']] = list( (bin(df['Col_B']).zfill(2) ) )
``````

If performance is important, use `numpy` with this solution:

``````d = df['Col_B'].values
m = 2
df[['Col_C','Col_D']]  = pd.DataFrame((((d[:,None] & (1 << np.arange(m)))) > 0).astype(int))
print (df)
Col_A  Col_B  Col_C  Col_D
0     a      1      1      0
1     b      2      0      1
2     c      0      0      0
``````

``````df = pd.DataFrame([['a', 1], ['b', 2], ['c', 0]], columns=["Col_A", "Col_B"])

df = pd.concat([df] * 1000, ignore_index=True)

In : %%timeit
...: df[['Col_C','Col_D']] = df['Col_B'].apply(lambda x: pd.Series(list(bin(x)[2:].zfill(2))))
...:
609 ms ± 14.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In : %%timeit
...: d = df['Col_B'].values
...: m = 2
...: df[['Col_C','Col_D']]  = pd.DataFrame((((d[:,None] & (1 << np.arange(m)))) > 0).astype(int))
...:
618 µs ± 26.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
``````