Data Conversion Error while applying a function to each row in pandas Python
<p>I have a data frame in pandas in python which resembles something like this -    </p>

<pre><code>    contest_login_count  contest_participation_count  ipn_ratio
0                    1                            1   0.000000
1                    3                            3   0.083333
2                    3                            3   0.000000
3                    3                            3   0.066667
4                    5                           13   0.102804
5                    2                            3   0.407407
6                    1                            3   0.000000
7                    1                            2   0.000000
8                   53                           91   0.264151
9                    1                            2   0.000000
</code></pre>

<p>Now I want to apply a function to each row of this dataframe The function is written as this - </p>

<pre><code>def findCluster(clusterModel,data):
    return clusterModel.predict(data)
</code></pre>

<p>I apply this function to each row in this manner - </p>

<pre><code>df_fil.apply(lambda x : findCluster(cluster_all,x.reshape(1,-1)),axis=1)
</code></pre>

<p>When I run this code, I get a warning saying - </p>

<blockquote>
  <p>DataConversionWarning: Data with input dtype object was converted to float64.</p>
  
  <p>warnings.warn(msg, DataConversionWarning)</p>
</blockquote>

<p>This warning is printed once for each row. Since, I have around 450K rows in my data frame, my computer hangs while printing all these warning messages that too on ipython notebook.</p>

<p>But to test my function I created a dummy dataframe and tried applying the same function on that and it works well. Here is the code for that - </p>

<pre><code>t = pd.DataFrame([[10.35,100.93,0.15],[10.35,100.93,0.15]])
t.apply(lambda x:findCluster(cluster_all,x.reshape(1,-1)),axis=1)
</code></pre>

<p>The output to this is - </p>

<pre><code>   0  1  2
0  4  4  4
1  4  4  4
</code></pre>

<p>Can anyone suggest what am I doing wrong or what can I change to make this error go away?</p>

<p>I think there is problem <code>dtype</code> of some column is not <code>float</code>.</p>

<p>You need cast it by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html" rel="nofollow"><code>astype</code></a>:</p>

<pre><code>df['colname'] = df['colname'].astype(float)
</code></pre>

python function pandas dataframe apply