How to add new column with handling nan value
<p>I have a dataframe like this</p>

<pre><code>    A   B
0   a   1
1   b   2
2   c   3
3   d  nan
4   e  nan
</code></pre>

<p>I would like to add column C like below</p>

<pre><code>    A   B    C
0   a   1    a1
1   b   2    b2
2   c   3    c3
3   d  nan   d
4   e  nan   e
</code></pre>

<p>So I tried </p>

<pre><code>df["C"]=df.A+df.B
</code></pre>

<p>but It returns </p>

<pre><code> C
a1
b2
c3
nan
nan
</code></pre>

<p>How can get correct result?</p>

<p>In your code, I think the data type of the element in the dataframe is str, so, try <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html?highlight=fillna#pandas.DataFrame.fillna" rel="nofollow">fillna</a>.</p>

<pre><code>In [10]: import pandas as pd
In [11]: import numpy as np
In [12]: df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'], 
                            'B': ['1', '2', '3', np.nan, np.nan]})

In [13]: df.B.fillna('')
Out[13]: 
0    1
1    2
2    3
3     
4     
Name: B, dtype: object

In [14]: df
Out[14]: 
   A    B
0  a    1
1  b    2
2  c    3
3  d  NaN
4  e  NaN

[5 rows x 2 columns]

In [15]: df.B = df.B.fillna('')

In [16]: df["C"]=df.A+df.B

In [17]: df
Out[17]: 
   A  B   C
0  a  1  a1
1  b  2  b2
2  c  3  c3
3  d      d
4  e      e

[5 rows x 3 columns]
</code></pre>

python pandas