title: Reading a text file and calculating probability and Shannon's entropy
body: <p>I have a text file (tab separated) and I need to calculate the probability and entropy for each column in the text file. Here is what my text file looks like:</p>
  
  <pre><code>aaa 0.0520852296    0.1648703511    0.1648703511
  bbb 0.1062639955    0.1632039268    0.1632039268
  ccc 1.4112745088    4.3654577641    4.3654577641
  ddd 0.4992644913    0.1648703511    0.1648703511
  eeee    0.169058175 0.1632039268    0.1632039268
  </code></pre>
  
  <p>and so I can calculate the probability using the following code:</p>
  
  <pre><code>import pandas as pd
  f=open(mydata,'r')
  df = pd.DataFrame(pd.read_csv(f, sep='\t', header=None, names=['val1', 'val2', 'val3']))
  print(df)
  df.loc[:,"val1":"val3"] = df.loc[:,"val1":"val3"].div(df.sum(axis=0), axis=1)
  print(df)
  </code></pre>
  
  <p>which outputs,</p>
  
  <pre><code>aaa 0.0232736716    0.0328321936    0.0328321936
  bbb 0.0474828153    0.0325003428    0.0325003428
  ccc 0.6306113983    0.8693349271    0.8693349271
  ddd 0.2230904597    0.0328321936    0.0328321936
  eeee    0.0755416551    0.0325003428    0.0325003428
  </code></pre>
  
  <p>And on that output I want to calculate the entropy and gave me the results as output file, and so I have the following code</p>
  
  <pre><code>import math
  entropy = - sum([ p * math.log(p) / math.log(2.0) for p in df ])
  </code></pre>
  
  <p>But I get the following error message:</p>
  
  <pre><code>TypeError: a float is required
  </code></pre>
  
  <p>Any help is much appreciated.
  Thank you all</p>