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) ) )