<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<p dir="auto">Case 1</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
df = pd.DataFrame({
               'a' : [1,1,1,2,2,2,3,3,3],
               'b' : [1,2,3,4,5,6,7,8,9],
})
df.groupby('a', as_index=False).agg({'a': 'count'})"><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-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({
               <span class="pl-s">'a'</span> : [<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">3</span>,<span class="pl-c1">3</span>],
               <span class="pl-s">'b'</span> : [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">5</span>,<span class="pl-c1">6</span>,<span class="pl-c1">7</span>,<span class="pl-c1">8</span>,<span class="pl-c1">9</span>],
})
<span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'a'</span>, <span class="pl-s1">as_index</span><span class="pl-c1">=</span><span class="pl-c1">False</span>).<span class="pl-en">agg</span>({<span class="pl-s">'a'</span>: <span class="pl-s">'count'</span>})</pre></div>
<p dir="auto">Case 2</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
df = pd.DataFrame({
               'a' : [1,1,1,2,2,2,3,3,3],
               'b' : [1,2,3,4,5,6,7,8,9],
})
df.groupby(['a']).agg({'a': 'count'}).reset_index(level=0)"><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-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({
               <span class="pl-s">'a'</span> : [<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">3</span>,<span class="pl-c1">3</span>],
               <span class="pl-s">'b'</span> : [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">5</span>,<span class="pl-c1">6</span>,<span class="pl-c1">7</span>,<span class="pl-c1">8</span>,<span class="pl-c1">9</span>],
})
<span class="pl-s1">df</span>.<span class="pl-en">groupby</span>([<span class="pl-s">'a'</span>]).<span class="pl-en">agg</span>({<span class="pl-s">'a'</span>: <span class="pl-s">'count'</span>}).<span class="pl-en">reset_index</span>(<span class="pl-s1">level</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)</pre></div>
<p dir="auto">It returns "ValueError: cannot insert a, already exists" for both caes.</p>
<h4 dir="auto">Problem description</h4>
<p dir="auto">So far, I could found 2 functions which calls "insert" function without "allow_duplicates=True" option.<br>
One is in  "def _insert_inaxis_grouper_inplace" from "pandas/core/groupby.py"</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="result.insert(0, name, lev)"><pre class="notranslate"><span class="pl-s1">result</span>.<span class="pl-en">insert</span>(<span class="pl-c1">0</span>, <span class="pl-s1">name</span>, <span class="pl-s1">lev</span>)</pre></div>
<p dir="auto">and another is in "def reset_index" from "pandas/core/frame.py"</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" new_obj.insert(0, name, level_values)"><pre class="notranslate"> <span class="pl-s1">new_obj</span>.<span class="pl-en">insert</span>(<span class="pl-c1">0</span>, <span class="pl-s1">name</span>, <span class="pl-s1">level_values</span>)</pre></div>
<p dir="auto">For both cases, it result in ValueError as I wrote in Code section.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I think for both cases it should at least return some kind of dataframe without returning ValueError.</p>
<ol dir="auto">
<li>It should add a new column even it was duplicated.</li>
<li>It should add a new column with renaming it (say a_1 or a_x as new column).</li>
</ol>
<p dir="auto">My concern is</p>
<ol dir="auto">
<li>Is this a bug?</li>
</ol>
<ul dir="auto">
<li>Should we supposed to aggrecate for "as_index=False" dataframe or do "reset_index" to aggregated dataframe?</li>
<li>(i.e. Are there any other way to rename column before doing reset_index or agg?)</li>
</ul>
<ol start="2" dir="auto">
<li>If this were a bug, how could I fix it?</li>
</ol>
<ul dir="auto">
<li>I could add "allow_duplicate=True" option for both lines.</li>
<li>Or, I could rename column by adding "_x" or "_dup" or any kind of suffix.</li>
</ul>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.4.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.8.0-53-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: ja_JP.UTF-8<br>
LOCALE: ja_JP.UTF-8</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: 3.5.0<br>
pip: 9.0.3<br>
setuptools: 39.0.1<br>
Cython: 0.28.1<br>
numpy: 1.14.2<br>
scipy: 1.0.1<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.2.1<br>
sphinx: 1.7.2<br>
patsy: 0.5.0<br>
dateutil: 2.7.2<br>
pytz: 2018.3<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.4<br>
feather: None<br>
matplotlib: 2.2.2<br>
openpyxl: 2.5.1<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.2.1<br>
bs4: 4.6.0<br>
html5lib: 1.0.1<br>
sqlalchemy: 1.2.5<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">From <a href="http://stackoverflow.com/questions/17979327/drop-rows-with-multiple-keys-in-pandas/17979614?noredirect=1#comment26286280_17979614" rel="nofollow">this</a> SO question.</p>
<p dir="auto">The user had two dataframes and wanted to use the second as the values argument to <code class="notranslate">isin</code>.</p>
<p dir="auto">We'd need to think about how to handle the other index.  In this case the user only cared about the columns, not the index labels.</p>
<p dir="auto">Previous issues/PRs: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="16809406" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/4258" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/4258/hovercard" href="https://github.com/pandas-dev/pandas/pull/4258">#4258</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="16637967" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/4211" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/4211/hovercard" href="https://github.com/pandas-dev/pandas/issues/4211">#4211</a></p>