<p dir="auto">related <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3072615" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/739" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/739/hovercard" href="https://github.com/pandas-dev/pandas/issues/739">#739</a></p>
<p dir="auto">Have a look at this example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
from StringIO import StringIO
print &quot;Pandas version %s\n\n&quot; % pd.__version__

data1 = &quot;&quot;&quot;idx,metric
0,2.1
1,2.5
2,3&quot;&quot;&quot;

data2 = &quot;&quot;&quot;idx,metric
0,2.7
1,2.2
2,2.8&quot;&quot;&quot;

df1 = pd.read_csv(StringIO(data1))
df2 = pd.read_csv(StringIO(data2))
concatenated = pd.concat([df1, df2], ignore_index=True)
merged = concatenated.groupby(&quot;idx&quot;).agg([np.mean, np.std])

print merged
print merged.sort('metric')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-v">StringIO</span> <span class="pl-k">import</span> <span class="pl-v">StringIO</span>
<span class="pl-k">print</span> <span class="pl-s">"Pandas version %s<span class="pl-cce">\n</span><span class="pl-cce">\n</span>"</span> <span class="pl-c1">%</span> <span class="pl-s1">pd</span>.<span class="pl-s1">__version__</span>

<span class="pl-s1">data1</span> <span class="pl-c1">=</span> <span class="pl-s">"""idx,metric</span>
<span class="pl-s">0,2.1</span>
<span class="pl-s">1,2.5</span>
<span class="pl-s">2,3"""</span>

<span class="pl-s1">data2</span> <span class="pl-c1">=</span> <span class="pl-s">"""idx,metric</span>
<span class="pl-s">0,2.7</span>
<span class="pl-s">1,2.2</span>
<span class="pl-s">2,2.8"""</span>

<span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-v">StringIO</span>(<span class="pl-s1">data1</span>))
<span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-v">StringIO</span>(<span class="pl-s1">data2</span>))
<span class="pl-s1">concatenated</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">concat</span>([<span class="pl-s1">df1</span>, <span class="pl-s1">df2</span>], <span class="pl-s1">ignore_index</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">merged</span> <span class="pl-c1">=</span> <span class="pl-s1">concatenated</span>.<span class="pl-en">groupby</span>(<span class="pl-s">"idx"</span>).<span class="pl-en">agg</span>([<span class="pl-s1">np</span>.<span class="pl-s1">mean</span>, <span class="pl-s1">np</span>.<span class="pl-s1">std</span>])

<span class="pl-k">print</span> <span class="pl-s1">merged</span>
<span class="pl-k">print</span> <span class="pl-s1">merged</span>.<span class="pl-en">sort</span>(<span class="pl-s">'metric'</span>)</pre></div>
<p dir="auto">and its output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python test.py 
Pandas version 0.11.0


     metric          
       mean       std
idx                  
0      2.40  0.424264
1      2.35  0.212132
2      2.90  0.141421
Traceback (most recent call last):
  File &quot;test.py&quot;, line 22, in &lt;module&gt;
    print merged.sort('metric')
  File &quot;/***/Python-2.7.3/lib/python2.7/site-packages/pandas/core/frame.py&quot;, line 3098, in sort
    inplace=inplace)
  File &quot;/***/Python-2.7.3/lib/python2.7/site-packages/pandas/core/frame.py&quot;, line 3153, in sort_index
    % str(by))
ValueError: Cannot sort by duplicate column metric"><pre class="notranslate"><code class="notranslate">$ python test.py 
Pandas version 0.11.0


     metric          
       mean       std
idx                  
0      2.40  0.424264
1      2.35  0.212132
2      2.90  0.141421
Traceback (most recent call last):
  File "test.py", line 22, in &lt;module&gt;
    print merged.sort('metric')
  File "/***/Python-2.7.3/lib/python2.7/site-packages/pandas/core/frame.py", line 3098, in sort
    inplace=inplace)
  File "/***/Python-2.7.3/lib/python2.7/site-packages/pandas/core/frame.py", line 3153, in sort_index
    % str(by))
ValueError: Cannot sort by duplicate column metric
</code></pre></div>
<p dir="auto">The problem here is not that there is a duplicate column <code class="notranslate">metric</code> as stated by the error message. The problem is that there are still two sub-levels. The solution in this case is to use</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="merged.sort([('metric', 'mean')])"><pre class="notranslate"><span class="pl-s1">merged</span>.<span class="pl-en">sort</span>([(<span class="pl-s">'metric'</span>, <span class="pl-s">'mean'</span>)])</pre></div>
<p dir="auto">for sorting by the mean of the metric. It took myself quite a while to figure this out. First of all, the error message should be more clear in this case. Then, maybe I was too stupid, but I could not find the solution in the docs, but within a thread on StackOverflow. Looks like the error message above is the result of an over-generalized condition around <a href="https://github.com/pydata/pandas/blob/v0.12.0rc1/pandas/core/frame.py#L3269">https://github.com/pydata/pandas/blob/v0.12.0rc1/pandas/core/frame.py#L3269</a></p>
<p dir="auto">When iterating through several dimension columns and doing a groupby function, it often works. However, for certain columns it will not and instead I get an error. The error is:</p>
<p dir="auto">ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().</p>
<p dir="auto">The only thing different about these specific columns is that they are unique. However, we won't often be aware if they are unique. I'm hoping this isn't the issue and if it is, is there a work around?</p>
<p dir="auto">Thanks!</p>
<p dir="auto">Below is the full traceback.</p>
<hr>
<p dir="auto">ValueError                                Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 df_grp = df.groupby('post_id').sum()</p>
<p dir="auto">/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze)<br>
3157         axis = self._get_axis_number(axis)<br>
3158         return groupby(self, by=by, axis=axis, level=level, as_index=as_index,<br>
-&gt; 3159                        sort=sort, group_keys=group_keys, squeeze=squeeze)<br>
3160<br>
3161     def asfreq(self, freq, method=None, how=None, normalize=False):</p>
<p dir="auto">/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in groupby(obj, by, *_kwds)<br>
1197         raise TypeError('invalid type: %s' % type(obj))<br>
1198<br>
-&gt; 1199     return klass(obj, by, *_kwds)<br>
1200<br>
1201</p>
<p dir="auto">/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in <strong>init</strong>(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze)<br>
386         if grouper is None:<br>
387             grouper, exclusions, obj = _get_grouper(obj, keys, axis=axis,<br>
--&gt; 388                                                     level=level, sort=sort)<br>
389<br>
390         self.obj = obj</p>
<p dir="auto">/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in _get_grouper(obj, key, axis, level, sort)<br>
2152             in_axis, name = False, None<br>
2153<br>
-&gt; 2154         if is_categorical_dtype(gpr) and len(gpr) != len(obj):<br>
2155             raise ValueError("Categorical dtype grouper must have len(grouper) == len(data)")<br>
2156</p>
<p dir="auto">/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in <strong>nonzero</strong>(self)<br>
712         raise ValueError("The truth value of a {0} is ambiguous. "<br>
713                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."<br>
--&gt; 714                          .format(self.<strong>class</strong>.<strong>name</strong>))<br>
715<br>
716     <strong>bool</strong> = <strong>nonzero</strong></p>
<p dir="auto">ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().</p>