title: How to split a dataframe column into multiple columns
body: <p>After much prodding I am starting migrating my R scripts to Python. Most of my work in R involved data frames, and I am using the <code>DataFrame</code> object from the pandas package. In my script I need to read in a csv file and import the data into a <code>DataFrame</code> object. Next I need to convert the hex values into a column labelled <code>DATA</code> into bitwise data, and then create 16 new columns, one for each bit.</p>
  <p>My example input data in file <code>test.txt</code> looks as follows,</p>
  <blockquote>
  <p>PREFIX,TEST,ZONE,ROW,COL,DATA</p>
  <p>6_6,READ,0,  0,  0,BFED</p>
  <p>6_6,READ,0,  1,  0,BB7D</p>
  <p>6_6,READ,0,  2,  0,FFF7</p>
  <p>6_6,READ,0,  3,  0,E7FF</p>
  <p>6_6,READ,0,  4,  0,FBF8</p>
  <p>6_6,READ,0,  5,  0,DE75</p>
  <p>6_6,READ,0,  6,  0,DFFE</p>
  </blockquote>
  <p>My python script <code>test.py</code> is as follows,</p>
  <pre><code>import glob
  
  import pandas as pd
  
  import numpy as np
  
  fname = 'test.txt'
  
  df = pd.read_csv(fname, comment=&quot;#&quot;)
  
  dfs = df[df.TEST == 'READ']
  
  # function to convert the hexstring into a binary string
  
  def hex2bin(hstr):
  
      return bin(int(hstr,16))[2:]
  
  
  # convert the hexstring in column DATA to binarystring ROWDATA
  
  dfs['BINDATA'] = dfs['DATA'].apply(hex2bin)
  
  # get rid of the column DATA
  
  del dfs['DATA']
  </code></pre>
  <p>When I run this script, and inspect the object <code>dfs</code>, I get the following,</p>
  <blockquote>
  <p>PREFIX  TEST  ZONE  ROW  COL           BINDATA</p>
  <p>0    6_6  READ     0    0    0  1011111111101101</p>
  <p>1    6_6  READ     0    1    0  1011101101111101</p>
  <p>2    6_6  READ     0    2    0  1111111111110111</p>
  <p>3    6_6  READ     0    3    0  1110011111111111</p>
  <p>4    6_6  READ     0    4    0  1111101111111000</p>
  <p>5    6_6  READ     0    5    0  1101111001110101</p>
  <p>6    6_6  READ     0    6    0  1101111111111110</p>
  <blockquote>
  <blockquote>
  </blockquote>
  </blockquote>
  </blockquote>
  <p>So now I am not sure how to split the column named <code>BINDATA</code> into 16 new columns (could be named B0, B0, B2, ...., B15). Any help will be appreciated.</p>
  <p>Thanks &amp; Regards,</p>
  <p>Derric.</p>