<p dir="auto">hello<br>
When using the mighty groupby function, I found myself quite disappointed by something:<br>
indeed, it is very useful regarding operations, but one of the most basic one is not mathematical.<br>
It is about getting the values associated to a column (or series of columns) per group.</p>
<p dir="auto">Maybe I missed something about the split-apply-combine strategy*, but I don't get how to retrieve simply the content of some rows for all or part of groups.<br>
get_group() does this beautifully for one but not for many (i tried to pass sets, lists at no effect),<br>
I can come up easily with something like:<br>
groups_dict= {k: list(grouped.get_group(k).loc[:,idcolumn]) for k in grouped.groups.keys()}</p>
<p dir="auto">But I guess this is not computationally efficient, plus it has to be repeated for all columns you want to aggregate.</p>
<p dir="auto">Are there any better methods or wouldn't this be relevant to have a setting to output the actual values instead of an index?</p>
<p dir="auto">I actually came up with another solution (agg_df being the original dataframe, you may need resetting the df index in case you used some exploding strategy):</p>
<p dir="auto">groups_dict2= {k: list(agg_df2.loc[v,'n']) for k,v in grouped.indices.items()}</p>
<p dir="auto">It feels slower but it is maybe more error proof?</p>
<p dir="auto">Anyway, a function using all the hidden workings in Pandas would be far better I guess.</p>
<p dir="auto">Best Regards</p>
<ul dir="auto">
<li>(I am sorry, I try my best reading the docs, but Pandas is definitely not trivial to handle, docs are huge and when you think that you might find some problem's solution in one section or function domain, it can actually be something hugely different),</li>
</ul>
<p dir="auto">Issues:</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked="">  <code class="notranslate">Index.difference</code> and <code class="notranslate">symmetric_difference</code> raise for mixed types (solved by <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="162312131" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/13514" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/13514/hovercard" href="https://github.com/pandas-dev/pandas/pull/13514">#13514</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> <code class="notranslate">Index._get_consensus_name</code> incorrectly returns an empty index (solved by <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="162312131" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/13514" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/13514/hovercard" href="https://github.com/pandas-dev/pandas/pull/13514">#13514</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> bug in <code class="notranslate">Index.union</code> for non-unique indexes (see <a href="#issuecomment-226665776">comment</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> bug in <code class="notranslate">Index.intersection</code> for non-unique indexes (same <a href="#issuecomment-226665776">comment</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> inconsistent sorting in of mixed-int Indexes in python2 (see <a href="#issuecomment-233520619">comment</a>)</li>
</ul>
<hr>
<h3 dir="auto"><code class="notranslate">Index.difference</code> and <code class="notranslate">symmetric_difference</code> raise for mixed types</h3>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="idx1 = pd.Index([0, 1, 'A', 'B'])
idx2 = pd.Index([0, 2, 'A', 'C'])

idx1.difference(idx2)
...
  File &quot;/usr/local/lib64/python3.5/site-packages/pandas/indexes/base.py&quot;, line 1861, in difference
    theDiff = sorted(set(self) - set(other))
TypeError: unorderable types: str() &lt; int()

idx1.symmetric_difference(idx2)
...
  File &quot;/usr/local/lib64/python3.5/site-packages/pandas/indexes/base.py&quot;, line 1861, in difference
    theDiff = sorted(set(self) - set(other))
TypeError: unorderable types: str() &lt; int()"><pre class="notranslate"><span class="pl-s1">idx1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Index</span>([<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-s">'A'</span>, <span class="pl-s">'B'</span>])
<span class="pl-s1">idx2</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Index</span>([<span class="pl-c1">0</span>, <span class="pl-c1">2</span>, <span class="pl-s">'A'</span>, <span class="pl-s">'C'</span>])

<span class="pl-s1">idx1</span>.<span class="pl-en">difference</span>(<span class="pl-s1">idx2</span>)
...
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib64/python3.5/site-packages/pandas/indexes/base.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1861</span>, <span class="pl-s1">in</span> <span class="pl-s1">difference</span>
    <span class="pl-s1">theDiff</span> <span class="pl-c1">=</span> <span class="pl-en">sorted</span>(<span class="pl-en">set</span>(<span class="pl-s1">self</span>) <span class="pl-c1">-</span> <span class="pl-en">set</span>(<span class="pl-s1">other</span>))
<span class="pl-v">TypeError</span>: <span class="pl-en">unorderable</span> <span class="pl-s1">types</span>: <span class="pl-s1">str</span>() <span class="pl-c1">&lt;</span> <span class="pl-en">int</span>()

<span class="pl-s1">idx1</span>.<span class="pl-en">symmetric_difference</span>(<span class="pl-s1">idx2</span>)
...
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib64/python3.5/site-packages/pandas/indexes/base.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1861</span>, <span class="pl-s1">in</span> <span class="pl-s1">difference</span>
    <span class="pl-s1">theDiff</span> <span class="pl-c1">=</span> <span class="pl-en">sorted</span>(<span class="pl-en">set</span>(<span class="pl-s1">self</span>) <span class="pl-c1">-</span> <span class="pl-en">set</span>(<span class="pl-s1">other</span>))
<span class="pl-v">TypeError</span>: <span class="pl-en">unorderable</span> <span class="pl-s1">types</span>: <span class="pl-s1">str</span>() <span class="pl-c1">&lt;</span> <span class="pl-en">int</span>()</pre></div>
<p dir="auto">But <code class="notranslate">union</code> and <code class="notranslate">intersection</code> work:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="idx1.union(idx2)
Out[14]: Index([0, 1, 'A', 'B', 2, 'C'], dtype='object')
idx1.intersection(idx2)
Out[15]: Index([0, 'A'], dtype='object')"><pre class="notranslate"><span class="pl-s1">idx1</span>.<span class="pl-en">union</span>(<span class="pl-s1">idx2</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">14</span>]: <span class="pl-v">Index</span>([<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-s">'A'</span>, <span class="pl-s">'B'</span>, <span class="pl-c1">2</span>, <span class="pl-s">'C'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)
<span class="pl-s1">idx1</span>.<span class="pl-en">intersection</span>(<span class="pl-s1">idx2</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">15</span>]: <span class="pl-v">Index</span>([<span class="pl-c1">0</span>, <span class="pl-s">'A'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)</pre></div>
<h4 dir="auto">Expected Output</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="idx1.difference(idx2)
Out[]: Index([1, 'B'], dtype='object')

idx1.symmetric_difference(idx2)
Out[]: Index([1, 'B', 2, 'C'], dtype='object')"><pre class="notranslate"><span class="pl-s1">idx1</span>.<span class="pl-en">difference</span>(<span class="pl-s1">idx2</span>)
<span class="pl-v">Out</span>[<span class="pl-s1"></span>]: <span class="pl-v">Index</span>([<span class="pl-c1">1</span>, <span class="pl-s">'B'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)

<span class="pl-s1">idx1</span>.<span class="pl-en">symmetric_difference</span>(<span class="pl-s1">idx2</span>)
<span class="pl-v">Out</span>[<span class="pl-s1"></span>]: <span class="pl-v">Index</span>([<span class="pl-c1">1</span>, <span class="pl-s">'B'</span>, <span class="pl-c1">2</span>, <span class="pl-s">'C'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)</pre></div>
<h4 dir="auto">output of <code class="notranslate">pd.show_versions()</code></h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: 4a6621fcaa9a3b98172b90a69de574ec94b108df
python: 3.5.1.final.0
python-bits: 64
OS: Linux
machine: x86_64
byteorder: little

pandas: 0.18.1
..."><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: 4a6621fcaa9a3b98172b90a69de574ec94b108df
python: 3.5.1.final.0
python-bits: 64
OS: Linux
machine: x86_64
byteorder: little

pandas: 0.18.1
...
</code></pre></div>