Setting elements to None in pandas dataframe
<p>I'm not sure why this happens</p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame(np.arange(15).reshape(5,3),columns=list('ABC'))
&gt;&gt;&gt; df
    A   B   C
0   0   1   2
1   3   4   5
2   6   7   8
3   9  10  11
4  12  13  14
</code></pre>

<p>Assign <code>None</code> to elements in last row turns it into <code>NaN NaN NaN</code>:</p>

<pre><code>&gt;&gt;&gt; df.ix[5,:] = None
&gt;&gt;&gt; df
    A   B   C
0   0   1   2
1   3   4   5
2   6   7   8
3   9  10  11
4  12  13  14
5 NaN NaN NaN
</code></pre>

<p>Change two element in last column to 'nan'</p>

<pre><code>&gt;&gt;&gt; df.ix[:1,2] = 'nan'
&gt;&gt;&gt; df
    A   B    C
0   0   1  nan
1   3   4  nan
2   6   7    8
3   9  10   11
4  12  13   14
5 NaN NaN  NaN
</code></pre>

<p>Now last row becomes <code>NaN NaN  None</code></p>

<pre><code>&gt;&gt;&gt; df.ix[5,:] = None
&gt;&gt;&gt; df
    A   B     C
0   0   1   nan
1   3   4   nan
2   6   7     8
3   9  10    11
4  12  13    14
5 NaN NaN  None
</code></pre>

<p>It's because your dtypes are being changed after each assignment:</p>

<pre><code>In [7]: df = pd.DataFrame(np.arange(15).reshape(5,3),columns=list('ABC'))

In [8]: df.dtypes
Out[8]:
A    int32
B    int32
C    int32
dtype: object

In [9]: df.ix[5,:] = None

In [10]: df.dtypes
Out[10]:
A    float64
B    float64
C    float64
dtype: object

In [11]: df.ix[:1,2] = 'nan'
</code></pre>

<p>after that last assignment the <code>C</code> column has been implicitly converted to <code>object</code> (string) dtype:</p>

<pre><code>In [12]: df.dtypes
Out[12]:
A    float64
B    float64
C     object
dtype: object
</code></pre>

<p><a href="http://stackoverflow.com/questions/39434733/setting-elements-to-none-in-pandas-dataframe/39434877#comment66192432_39434877">@ayhan has written very neat answer as a comment</a>:</p>

<blockquote>
  <p>I think the main reason is for numerical columns, when you insert None
  or np.nan, it is converted to np.nan to have a Series of type float.
  For objects, it takes whatever is passed (if None, it uses None; if
  np.nan, it uses np.nan -
  <a href="http://pandas.pydata.org/pandas-docs/stable/missing_data.html#inserting-missing-data" rel="nofollow">docs</a>)</p>
  
  <p>(c) <a href="http://stackoverflow.com/users/2285236/ayhan">ayhan</a></p>
</blockquote>

<p>Here is a corresponding demo:</p>

<pre><code>In [39]: df = pd.DataFrame(np.arange(15).reshape(5,3),columns=list('ABC'))

In [40]: df.ix[4, 'A'] = None

In [41]: df.ix[4, 'C'] = np.nan

In [42]: df
Out[42]:
     A   B     C
0  0.0   1   2.0
1  3.0   4   5.0
2  6.0   7   8.0
3  9.0  10  11.0
4  NaN  13   NaN

In [43]: df.dtypes
Out[43]:
A    float64
B      int32
C    float64
dtype: object

In [44]: df.ix[0, 'C'] = 'a string'

In [45]: df
Out[45]:
     A   B         C
0  0.0   1  a string
1  3.0   4         5
2  6.0   7         8
3  9.0  10        11
4  NaN  13       NaN

In [46]: df.dtypes
Out[46]:
A    float64
B      int32
C     object
dtype: object
</code></pre>

<p>now we can use both <code>None</code> and <code>np.nan</code> for the <code>object</code> dtype:</p>

<pre><code>In [47]: df.ix[1, 'C'] = None

In [48]: df.ix[2, 'C'] = np.nan

In [49]: df
Out[49]:
     A   B         C
0  0.0   1  a string
1  3.0   4      None
2  6.0   7       NaN
3  9.0  10        11
4  NaN  13       NaN
</code></pre>

python pandas dataframe