Pandas: adding new column to existing Data Frame for grouping purposes
<p>I have a <code>pandas</code> Data Frame consisting of 2000 rows x 8 columns. I want to be able to group the first 4 columns together, as well as the other 4, but I can't figure out how. The purpose is to create a categorical bar plot, with colors assigned according to C1=C5, C2=C6, and so forth.</p>

<p>My Data Frame:</p>

<pre><code>In[1]: df.head(5)
Out[1]: 

    C1  C2  C3  C4  C5  C6  C7  C8
0   15  37  17  10  8   11  19  86
1   39  84  11  5   5   13  9   11
2   10  20  30  51  74  62  56  58
3   88  2   1   3   9   6   0   17
4   17  17  32  24  91  45  63  48
</code></pre>

<p>Do you suggest adding another column such as <code>df['Gr']</code> or what else?</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.MultiIndex.from_arrays.html" rel="nofollow"><code>MultiIndex.from_arrays</code></a>:</p>

<pre><code>df.columns = pd.MultiIndex.from_arrays([['a'] * 4 + ['b'] * 4 , df.columns])
print (df)
    a               b            
   C1  C2  C3  C4  C5  C6  C7  C8
0  15  37  17  10   8  11  19  86
1  39  84  11   5   5  13   9  11
2  10  20  30  51  74  62  56  58
3  88   2   1   3   9   6   0  17
4  17  17  32  24  91  45  63  48
</code></pre>

<p>Then you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.xs.html" rel="nofollow"><code>xs</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.bar.html" rel="nofollow"><code>DataFrame.plot.bar</code></a>:</p>

<pre><code>import matplotlib.pyplot as plt

f, a = plt.subplots(2,1)
df.xs('a', axis=1).plot.bar(ax=a[0])
df.xs('b', axis=1).plot.bar(ax=a[1])
plt.show()
</code></pre>

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

<hr>

<pre><code>import matplotlib.pyplot as plt

df.columns = pd.MultiIndex.from_arrays([['a'] * 4 + ['b'] * 4 , df.columns])
df.stack(0).T.plot.bar(rot='0', legend=False)

df.columns = ['a'] * 4 + ['b'] * 4
df = df.T.plot.bar(rot='0')

plt.show()
</code></pre>

python pandas dataframe grouping bar-chart