How to compare decimal numbers available in columns of pandas dataframe?
<p>I want to compare decimal values which are available in two columns of pandas dataframe.</p>

<p>I have a dataframe:</p>

<pre><code>data = {'AA' :{0:'-14.35',1:'632.0',2:'619.5',3:'352.35',4:'347.7',5:'100'},
        'BB' :{0:'-14.3500',1:'632.0000',2:'619.5000',3:'352.3500',4:'347.7000',5:'200'}
       }
df1 = pd.DataFrame(data)
print df1
</code></pre>

<p><strong>dataframe look like this :</strong></p>

<pre><code>       AA        BB
0  -14.35  -14.3500
1   632.0  632.0000
2   619.5  619.5000
3  352.35  352.3500
4   347.7  347.7000
5   100    200
</code></pre>

<p>I want to compare <code>AA</code> and <code>BB</code> columns. As shown in above dataframe, values of both columns are same except <strong>5th</strong> row. only issue is the trailing zeros. </p>

<p>If both <code>AA</code> and <code>BB</code> columns are same then I want result of these comparison in third column as a <code>Result</code> i.e. <code>True</code> or <code>False</code>.</p>

<p><strong>Expected Result :</strong>  </p>

<pre><code>       AA        BB   Result
0  -14.35  -14.35    True
1   632.0  632.0     True
2   619.5  619.5     True
3  352.35  352.35    True
4   347.7  347.7     True
5   100    200       False
</code></pre>

<p>How I can compare these decimal values?</p>

<p>You need cast column to <code>float</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html" rel="nofollow"><code>astype</code></a> and then compare columns, because <code>type</code> of values in columns is <code>string</code>. Then use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mask.html" rel="nofollow"><code>mask</code></a> and as condition use boolean column <code>Result</code>:</p>

<pre><code>print (type(df1.ix[0,'AA']))
&lt;class 'str'&gt;

print (type(df1.ix[0,'BB']))
&lt;class 'str'&gt;

df1['Result'] = df1.AA.astype(float) == df1.BB.astype(float)
df1.BB = df1.BB.mask(df1.Result,df1.AA)
print (df1)
       AA      BB Result
0  -14.35  -14.35   True
1   632.0   632.0   True
2   619.5   619.5   True
3  352.35  352.35   True
4   347.7   347.7   True
5     100     200  False
</code></pre>

<p>Another solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ix.html" rel="nofollow"><code>ix</code></a>:</p>

<pre><code>df1['Result'] = df1.AA.astype(float) == df1.BB.astype(float)
df1.ix[df1.Result, 'BB'] = df1.AA
print (df1)
       AA      BB Result
0  -14.35  -14.35   True
1   632.0   632.0   True
2   619.5   619.5   True
3  352.35  352.35   True
4   347.7   347.7   True
5     100     200  False
</code></pre>

<p><strong>Timings</strong>:</p>

<pre><code>#len(df) = 6k
df1 = pd.concat([df1]*1000).reset_index(drop=True)

In [31]: %timeit df1.ix[df1.Result, 'BB'] = df1.AA
The slowest run took 4.88 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 1.19 ms per loop

In [33]: %timeit df1.BB = df1.BB.mask(df1.Result,df1.AA)
1000 loops, best of 3: 900 Âµs per loop
</code></pre>

python pandas indexing dataframe condition