Pandas: Is there a way to use something like 'droplevel' and in process, rename the the other level using the dropped level labels as prefix/suffix?
<p>Screenshot of the query below:</p>

<p><a href="http://i.stack.imgur.com/dGNAV.png" rel="nofollow"><img src="http://i.stack.imgur.com/dGNAV.png" alt="Groupby Query"></a></p>

<p>Is there a way to easily drop the upper level column index and a have a single level with labels such as <code>points_prev_amax</code>, <code>points_prev_amin</code>, <code>gf_prev_amax</code>, <code>gf_prev_amin</code> and so on?  </p>

<p>Use <code>list comprehension</code> for set new column names:</p>

<pre><code>df.columns = ['_'.join(col) for col in df.columns]
</code></pre>

<p>Sample:</p>

<pre><code>df = pd.DataFrame({'A':[1,2,2,1],
                   'B':[4,5,6,4],
                   'C':[7,8,9,1],
                   'D':[1,3,5,9]})

print (df)
   A  B  C  D
0  1  4  7  1
1  2  5  8  3
2  2  6  9  5
3  1  4  1  9

df = df.groupby('A').agg([max, min])

print (['_'.join(col) for col in df.columns])
['B_max', 'B_min', 'C_max', 'C_min', 'D_max', 'D_min']

df.columns = ['_'.join(col) for col in df.columns]
print (df)
   B_max  B_min  C_max  C_min  D_max  D_min
A                                          
1      4      4      7      1      9      1
2      6      5      9      8      5      3
</code></pre>

<p>If need <code>prefix</code> simple swap items of tuples:</p>

<pre><code>df.columns = ['_'.join((col[1], col[0])) for col in df.columns]
print (df)
   max_B  min_B  max_C  min_C  max_D  min_D
A                                          
1      4      4      7      1      9      1
2      6      5      9      8      5      3
</code></pre>

<p>Another solution:</p>

<pre><code>df.columns = ['{}_{}'.format(i[1], i[0]) for i in df.columns]
print (df)
   max_B  min_B  max_C  min_C  max_D  min_D
A                                          
1      4      4      7      1      9      1
2      6      5      9      8      5      3
</code></pre>

<p>If <code>len</code> of columns is big (10^6), then rather use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.to_series.html" rel="nofollow"><code>to_series</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.join.html" rel="nofollow"><code>str.join</code></a>:</p>

<pre><code>df.columns = df.columns.to_series().str.join('_')
</code></pre>

python pandas rename multiple-columns multi-index