Pandas NaN introduced by pivot_table
<p>I have a table containing some countries and their KPI from the world-banks API. this looks like <a href="http://i.stack.imgur.com/ZL4bU.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/ZL4bU.jpg" alt="no nan values present"></a>. As you can see no nan values are present. </p>

<p>However, I need to pivot this table to bring int into the right shape for analysis. A <code>pd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id'])</code>
For some e.g. <code>TUERKEI</code> this works just fine:</p>

<p><a href="http://i.stack.imgur.com/QiO6Z.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/QiO6Z.jpg" alt="for turkey it works"></a>
But for most of the countries strange nan values are introduced. How can I prevent this?</p>

<p><a href="http://i.stack.imgur.com/Ylu9t.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/Ylu9t.jpg" alt="strange nan values"></a></p>

<p>I think the best understand <code>pivoting</code> is in small sample:</p>

<pre><code>import pandas as pd
import numpy as np

countryKPI = pd.DataFrame({'germanCName':['a','a','b','c','c'],
                           'indicator.id':['z','x','z','y','m'],
                           'value':[7,8,9,7,8]})

print (countryKPI)
  germanCName indicator.id  value
0           a            z      7
1           a            x      8
2           b            z      9
3           c            y      7
4           c            m      8

print (pd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id']))
             value               
indicator.id     m    x    y    z
germanCName                      
a              NaN  8.0  NaN  7.0
b              NaN  NaN  NaN  9.0
c              8.0  NaN  7.0  NaN
</code></pre>

<p>If need replace <code>NaN</code> to <code>0</code> add parameter <code>fill_value</code>:</p>

<pre><code>print (countryKPI.pivot_table(index='germanCName', 
                              columns='indicator.id', 
                              values='value', 
                              fill_value=0))
indicator.id  m  x  y  z
germanCName             
a             0  8  0  7
b             0  0  0  9
c             8  0  7  0
</code></pre>

<p>You can also check <a class='doc-link' href="http://stackoverflow.com/documentation/pandas/1463/reshaping-and-pivoting/4770/simple-pivoting#t=20160922071448883903">simple pivoting</a> and <a class='doc-link' href="http://stackoverflow.com/documentation/pandas/1463/reshaping-and-pivoting/4771/pivoting-with-aggregating#t=20160922071448883903">pivoting with aggregating</a>.</p>

python pandas pivot pivot-table