Convert pandas df multi index to columns
<p>first post here, any assistance is much appreciated.</p>

<p>After reading a csv file response from an api request using python urllib2, i am left with a multi index df. It contains 19 index' then only 2 'columns'.</p>

<p>How do i convert these 19 index' to additional columns please?</p>

<p>I tried resting the index, but with no luck.</p>

<p><code>from urllib2 import Request, urlopen, URLError
import pandas as pd</code></p>

<pre><code>url = 'URL string here'

response = urlopen(request)
df = pd.read_csv(response)
df.reset_index().head()
</code></pre>

<p>to be clear, for example if index 1 contains letters a,b,c,d, i would simply like to change this index to a column tittled 'letter' with now every row containing one of these letters. When i do reset_index, it does indeed populate every row with a letter, however, the column itself is still an index..</p>

<p>Edit.. .added more code this first part gets the df.</p>

<pre><code>from urllib2 import Request, urlopen, URLError
import pandas as pd

host = 'testapi.bmreports.com'
port = '443'
rep_name = 'DETSYSPRICES'
version = 'v1'
key = 'ldytgh1ylq0k92c'
sd = '2016-05-26'
sp = 20
criteria = (host,port,rep_name,version,key,sd,sp)
url = 'https://%s:%s/BMRS/%s/%s?APIKey=%s&amp;SettlementDate=%s&amp;SettlementPeriod=%d&amp;ServiceType=CSV' % criteria
request = Request(url)
#print url

response = urlopen(request)
df = pd.read_csv(response)
df


HDR INDICATIVE SYSTEM PRICE STACK DATA
BID 20160526    20  1   1   NaN NaN F   T   F   F   NaN 266.0329    -230.211    -230.211    -149.786    -149.786    -48.05  266.0329    1.00000 -48.050 -12782.88
2   T_DIDCB6    109615  -1  F   F   F   F   NaN 26.0000 -1.950  -1.950  -1.950  -1.950  -1.95   26.0000 0.98947 -1.929  -50.17
3   T_COSO-1    119674  -1  T   F   F   F   NaN 25.9000 -0.279  0.000   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
OFFER   20160526    20  1   T_WBURB-2   25968   2   F   F   F   F   0   46.0000 16.163  16.163  0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
2   T_WBURB-2   25968   1   F   F   F   F   0   46.0000 3.037   3.037   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
3   T_CNQPS-4   45744   1   F   F   F   F   0   50.0000 0.975   0.000   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
4   T_DAMC-1    85044   1   F   F   F   F   0   64.5000 4.583   4.583   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
5   T_DAMC-1    85045   1   F   F   F   F   0   64.5000 0.083   0.083   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
6   T_DAMC-1    85046   1   F   F   F   F   0   64.5000 22.000  22.000  0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
7   T_BAGE-1    33725   1   T   F   F   F   0   70.0000 1.583   1.583   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
8   T_SUTB-1    68274   1   T   F   F   F   0   72.0000 4.000   4.000   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
9   T_HUMR-1    99956   2   F   F   F   F   0   82.4700 9.250   9.250   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
10  T_HUMR-1    99956   1   F   F   F   F   0   82.4700 0.250   0.000   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
11  T_HUMR-1    99955   2   F   F   F   F   0   82.4700 0.647   0.647   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
12  T_HUMR-1    99955   1   F   F   F   F   0   82.4700 0.033   0.000   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
13  T_HUMR-1    99956   3   F   F   F   F   0   95.9700 10.317  10.317  0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
14  T_HUMR-1    99955   3   F   F   F   F   0   95.9700 0.004   0.004   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
15  T_FOYE-1    92982   1   T   F   F   F   0   103.0000    1.258   1.258   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
16  T_FOYE-1    92983   1   T   F   F   F   0   103.0000    7.500   7.500   0.000   0.000   0.00    0.0000  0.00000 0.000   0.00
FTR 19  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
</code></pre>

<p>in this second part, i attempt the reset...</p>

<pre><code>df.reset_index()
df.index  


MultiIndex(levels=[[u'BID', u'FTR', u'OFFER'], [19, 20160526], [20.0], [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0], [u'1', u'T_BAGE-1', u'T_CNQPS-4', u'T_COSO-1', u'T_DAMC-1', u'T_DIDCB6', u'T_FOYE-1', u'T_HUMR-1', u'T_SUTB-1', u'T_WBURB-2'], [25968.0, 33725.0, 45744.0, 68274.0, 85044.0, 85045.0, 85046.0, 92982.0, 92983.0, 99955.0, 99956.0, 109615.0, 119674.0], [-1.0, 1.0, 2.0, 3.0], [u'F', u'T'], [u'F', u'T'], [u'F'], [u'F'], [0.0], [25.9, 26.0, 46.0, 50.0, 64.5, 70.0, 72.0, 82.47, 95.97, 103.0, 266.0329], [-230.211, -1.95, -0.279, 0.004, 0.033, 0.083, 0.25, 0.647, 0.975, 1.258, 1.583, 3.037, 4.0, 4.583, 7.5, 9.25, 10.317, 16.163, 22.0], [-230.211, -1.95, 0.0, 0.004, 0.083, 0.647, 1.258, 1.583, 3.037, 4.0, 4.583, 7.5, 9.25, 10.317, 16.163, 22.0], [-149.786, -1.95, 0.0], [-149.786, -1.95, 0.0], [-48.05, -1.95, 0.0], [0.0, 26.0, 266.0329], [0.0, 0.98947, 1.0]],
           labels=[[0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1], [0, 1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1], [0, 5, 3, 9, 9, 2, 4, 4, 4, 1, 8, 7, 7, 7, 7, 7, 7, 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