input output floating number issue python
<p>Python seems to do funny things to floating point numbers, it produces different floating point numbers from the input i give it, i would like the floating numbers to stay the same as the input. </p>

<p>here i have a small test dataset:</p>

<pre><code>import pandas as pd

df = {'ID': ['H1','H2','H3','H4','H5','H6'],
      'Length': [72, 72, '', 72, 72,'' ],
      'AA1': ['C','C','C','C','C','C'],
      'AA2': ['W','W','W','W','W','W'],
       'Freq': [0.14532872, 0.141868512,0.138408304, 0.14532872,0.138408304, 0.138408304 ],
       'M':    [-282.0570386,-279.1090993,-276.16116,-282.0570386,-274.7748657,-274.6160337]}

df = pd.DataFrame(df)
</code></pre>

<p>it is <strong>supposed</strong> to look like : </p>

<pre><code>    df
  Out[2]: 
      AA1 AA2      Freq    ID     Length           M
   0   C   W  0.14532872   H1     72         -282.0570386
   1   C   W  0.141868512  H2     72         -279.1090993
   2   C   W  0.138408304  H3                -276.16116
   3   C   W  0.14532872   H4     72         -282.0570386
   4   C   W  0.138408304  H5     72         -274.7748657
   5   C   W  0.138408304  H6                -274.6160337
</code></pre>

<p>but it actually looks like this, <strong>notice the difference in floating numbers in columns 'Freq' and 'M':</strong></p>

<pre><code>df
Out[2]: 
     AA1 AA2      Freq     ID Length           M
  0   C   W     0.145329  H1     72     -282.057039
  1   C   W     0.141869  H2     72     -279.109099
  2   C   W     0.138408  H3            -276.161160
  3   C   W     0.145329  H4     72     -282.057039
  4   C   W     0.138408  H5     72      -274.774866
  5   C   W     0.138408  H6             -274.616034
</code></pre>

<p>And when i run my script to just simply filter out the rows i don't want:</p>

<pre><code>import pandas as pd


df = pd.read_csv('test.txt', sep='\t' )

df2 = df[(df['Length'] != 0 ) &amp; (df['AA1'] == 'C')&amp; (df['AA2']== 'C')]

df2.to_csv('results.txt', sep = '\t', index=False)
</code></pre>

<p>the 'results.txt' file contains weird floating numbers that's not the same as the input,  there must be a way to output the floating numbers as the input, but i couldn't find related topics online. </p>

<p>Floats are weird: <a href="https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/02Numerics/Double/paper.pdf" rel="nofollow">https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/02Numerics/Double/paper.pdf</a></p>

<p>It's not uncommon to see peculiar looking behavior out of them. If you're not doing any calculations on them, I suggest converting them to strings first, so they're stored in the format you want.  </p>

python floating-point