Convert Number To Binary And Store In Multiple Columns In Pandas Using Python
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
Performance (about 1000 times faster):
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)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module