Manipulating pandas series - empty rows in a column
<p>I apologize in advance for what I guess is a basic dataframe / series selection issue, but I am a newbie and a bit stuck.</p>

<p>I have the following data:</p>

<pre><code>seas  off
2000  ARI    0.569369
      ATL    0.553398
      BAL    0.554404
      BUF    0.571429
      CAR    0.600000
      CHI    0.560886
      CIN    0.454945
      CLE    0.573196
      DAL    0.572707
      DEN    0.612850
      DET    0.550696
</code></pre>

<p>The 'seas' then repeats for 2001 and so on for 2015. FWIW, when I try <code>df['off']</code> it doesn't return the "off" column.</p>

<p>Anyway, what I want to do is basically create a key for each number. To do this I want to copy the year down for each row so it and then to add it to "off" to get a key. So as follows:</p>

<pre><code>seas  off    value     key
2000  ARI    0.569369  2000ARI
2000  ATL    0.553398  2000ATL
2000  BAL    0.554404  2000BAL
2000  BUF    0.571429  2000BUF
2000  CAR    0.600000  2000CAR
...
...
2001  CHI    0.560886  2001CHI
2001  CIN    0.454945  2001CIN
2001  CLE    0.573196  2001CLE
2001  DAL    0.572707  2001DAL
2001  DEN    0.612850  2001DEN
2001  DET    0.550696  2001DET
</code></pre>

<p>Help much appreciated ...</p>

<p>John</p>

<p>My guess is that you don't have a DataFrame, but a Series with a MultiIndex.</p>

<pre><code>import io
import pandas as pd

data = io.StringIO('''\
seas  off       value
2000  ARI    0.569369
2000  ATL    0.553398
2000  BAL    0.554404
2000  BUF    0.571429
2000  CAR    0.600000
2000  CHI    0.560886
2000  CIN    0.454945
2000  CLE    0.573196
2000  DAL    0.572707
2000  DEN    0.612850
2000  DET    0.550696
''')
df = pd.read_csv(data, delim_whitespace=True).set_index(['seas', 'off']).squeeze()
</code></pre>

<p>In that case, here's what you can do. First, make <code>seas</code> and <code>off</code> into columns of a DataFrame:</p>

<pre><code>df = df.reset_index()
</code></pre>

<p>Then create a column <code>key</code> by concatenating the <code>seas</code> and <code>off</code> columns:</p>

<pre><code>df['key'] = df['seas'].astype(str) + df['off']
</code></pre>

<p>Finally, send <code>seas</code> and <code>off</code> back to the index:</p>

<pre><code>df = df.set_index(['seas', 'off'])
</code></pre>

<p>Output:</p>

<pre><code>             value      key
seas off                   
2000 ARI  0.569369  2000ARI
     ATL  0.553398  2000ATL
     BAL  0.554404  2000BAL
     BUF  0.571429  2000BUF
     CAR  0.600000  2000CAR
     CHI  0.560886  2000CHI
     CIN  0.454945  2000CIN
     CLE  0.573196  2000CLE
     DAL  0.572707  2000DAL
     DEN  0.612850  2000DEN
     DET  0.550696  2000DET
</code></pre>

python pandas dataframe series