<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="import pandas as pd

df = pd.DataFrame([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;], columns=[&quot;test&quot;])

print(df[&quot;test&quot;].value_counts())
"><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-s">"b"</span>, <span class="pl-s">"c"</span>], <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">"test"</span>])

<span class="pl-en">print</span>(<span class="pl-s1">df</span>[<span class="pl-s">"test"</span>].<span class="pl-en">value_counts</span>())</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Using value_counts in a testsuite can be a problem, when the resulting values have the same count as they permutade on each call, e.g.:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python pandas_value_counts.py
a    1
b    1
c    1
Name: test, dtype: int64

$ python pandas_value_counts.py
c    1
a    1
b    1
Name: test, dtype: int64
"><pre class="notranslate"><code class="notranslate">$ python pandas_value_counts.py
a    1
b    1
c    1
Name: test, dtype: int64

$ python pandas_value_counts.py
c    1
a    1
b    1
Name: test, dtype: int64

</code></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">Some stable/deterministic output or optionally additionally sorting of the keys, if they have the same counts</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 32
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: None.None
<p dir="auto">pandas: 0.19.2<br>
nose: 1.3.7<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.25.2<br>
numpy: 1.11.3<br>
scipy: 0.18.1<br>
statsmodels: 0.6.1<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: 1.5.1<br>
patsy: 0.4.1<br>
dateutil: 2.6.0<br>
pytz: 2016.10<br>
blosc: None<br>
bottleneck: 1.2.0<br>
tables: 3.2.2<br>
numexpr: 2.6.1<br>
matplotlib: 2.0.0<br>
openpyxl: 2.4.1<br>
xlrd: 1.0.0<br>
xlwt: 1.2.0<br>
xlsxwriter: 0.9.6<br>
lxml: 3.7.2<br>
bs4: 4.5.3<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.5<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.4<br>
boto: 2.45.0<br>
pandas_datareader: None</p>
</details>
<p dir="auto">Hello,</p>
<p dir="auto">I'm trying to make a new DataFrame that contains the value counts of a column of an existing DataFrame (<a href="https://github.com/pydata/pandas/files/181629/spreadsheet.xlsx">spreadsheet.xlsx</a>), but I want the rows in the new DataFrame to be in the same order as the old one.</p>
<p dir="auto">When I do:</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.read_excel('./spreadsheet.xlsx')
print(df[0].value_counts(sort=False))"><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-en">read_excel</span>(<span class="pl-s">'./spreadsheet.xlsx'</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df</span>[<span class="pl-c1">0</span>].<span class="pl-en">value_counts</span>(<span class="pl-s1">sort</span><span class="pl-c1">=</span><span class="pl-c1">False</span>))</pre></div>
<p dir="auto">I get the DataFrame:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="h4  8
ct1 6
f2  2
s1  2
EST2    2
f5  2
E4  8
h2  8
hd2 7
f3  2
ART1    2
s2  2
f1  2
h3  8
EST1    2
s3  2
E6  8
ART2    2
DGT2    2
ct2 6
s4  2
ct3 6
f4  2
DGT1    2
s5  2"><pre class="notranslate"><span class="pl-s1">h4</span>  <span class="pl-c1">8</span>
<span class="pl-s1">ct1</span> <span class="pl-c1">6</span>
<span class="pl-s1">f2</span>  <span class="pl-c1">2</span>
<span class="pl-s1">s1</span>  <span class="pl-c1">2</span>
<span class="pl-v">EST2</span>    <span class="pl-c1">2</span>
<span class="pl-s1">f5</span>  <span class="pl-c1">2</span>
<span class="pl-v">E4</span>  <span class="pl-c1">8</span>
<span class="pl-s1">h2</span>  <span class="pl-c1">8</span>
<span class="pl-s1">hd2</span> <span class="pl-c1">7</span>
<span class="pl-s1">f3</span>  <span class="pl-c1">2</span>
<span class="pl-v">ART1</span>    <span class="pl-c1">2</span>
<span class="pl-s1">s2</span>  <span class="pl-c1">2</span>
<span class="pl-s1">f1</span>  <span class="pl-c1">2</span>
<span class="pl-s1">h3</span>  <span class="pl-c1">8</span>
<span class="pl-v">EST1</span>    <span class="pl-c1">2</span>
<span class="pl-s1">s3</span>  <span class="pl-c1">2</span>
<span class="pl-v">E6</span>  <span class="pl-c1">8</span>
<span class="pl-v">ART2</span>    <span class="pl-c1">2</span>
<span class="pl-v">DGT2</span>    <span class="pl-c1">2</span>
<span class="pl-s1">ct2</span> <span class="pl-c1">6</span>
<span class="pl-s1">s4</span>  <span class="pl-c1">2</span>
<span class="pl-s1">ct3</span> <span class="pl-c1">6</span>
<span class="pl-s1">f4</span>  <span class="pl-c1">2</span>
<span class="pl-v">DGT1</span>    <span class="pl-c1">2</span>
<span class="pl-s1">s5</span>  <span class="pl-c1">2</span></pre></div>
<p dir="auto">When what I really want is:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="h2  8
h3  8
h4  8
hd2 7
E4  8
E6  8
ct1 6
.
.
."><pre class="notranslate"><span class="pl-s1">h2</span>  <span class="pl-c1">8</span>
<span class="pl-s1">h3</span>  <span class="pl-c1">8</span>
<span class="pl-s1">h4</span>  <span class="pl-c1">8</span>
<span class="pl-s1">hd2</span> <span class="pl-c1">7</span>
<span class="pl-v">E4</span>  <span class="pl-c1">8</span>
<span class="pl-v">E6</span>  <span class="pl-c1">8</span>
<span class="pl-s1">ct1</span> <span class="pl-c1">6</span>
.
.
.</pre></div>
<p dir="auto">...because that's the order in which the values occur in the original DataFrame.</p>
<p dir="auto">I can't tell how it's sorting them, but it is somehow. Is this the expected behavior?</p>
<p dir="auto">Thanks</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Installed versions:
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.1
setuptools: 20.3
Cython: 0.23.4
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.0
dateutil: 2.5.0
pytz: 2016.1
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.4.6
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0"><pre class="notranslate"><span class="pl-v">Installed</span> <span class="pl-s1">versions</span>:
<span class="pl-s1">commit</span>: <span class="pl-c1">None</span>
<span class="pl-s1">python</span>: <span class="pl-c1">3.5</span>.<span class="pl-c1">1.</span><span class="pl-s1">final</span>.<span class="pl-c1">0</span>
<span class="pl-s1">python</span><span class="pl-c1">-</span><span class="pl-s1">bits</span>: <span class="pl-c1">64</span>
<span class="pl-v">OS</span>: <span class="pl-v">Darwin</span>
<span class="pl-v">OS</span><span class="pl-c1">-</span><span class="pl-s1">release</span>: <span class="pl-c1">15.3</span><span class="pl-c1">.0</span>
<span class="pl-s1">machine</span>: <span class="pl-s1">x86_64</span>
<span class="pl-s1">processor</span>: <span class="pl-s1">i386</span>
<span class="pl-s1">byteorder</span>: <span class="pl-s1">little</span>
<span class="pl-v">LC_ALL</span>: <span class="pl-c1">None</span>
<span class="pl-v">LANG</span>: <span class="pl-c1">None</span>

<span class="pl-s1">pandas</span>: <span class="pl-c1">0.18</span>.<span class="pl-c1">0</span>
<span class="pl-s1">nose</span>: <span class="pl-c1">1.3</span>.<span class="pl-c1">7</span>
<span class="pl-s1">pip</span>: <span class="pl-c1">8.1</span>.<span class="pl-c1">1</span>
<span class="pl-s1">setuptools</span>: <span class="pl-c1">20.3</span>
<span class="pl-v">Cython</span>: <span class="pl-c1">0.23</span>.<span class="pl-c1">4</span>
<span class="pl-s1">numpy</span>: <span class="pl-c1">1.10</span>.<span class="pl-c1">4</span>
<span class="pl-s1">scipy</span>: <span class="pl-c1">0.17</span>.<span class="pl-c1">0</span>
<span class="pl-s1">statsmodels</span>: <span class="pl-c1">None</span>
<span class="pl-s1">xarray</span>: <span class="pl-c1">None</span>
<span class="pl-v">IPython</span>: <span class="pl-c1">4.1</span>.<span class="pl-c1">2</span>
<span class="pl-s1">sphinx</span>: <span class="pl-c1">1.3</span>.<span class="pl-c1">5</span>
<span class="pl-s1">patsy</span>: <span class="pl-c1">0.4</span>.<span class="pl-c1">0</span>
<span class="pl-s1">dateutil</span>: <span class="pl-c1">2.5</span>.<span class="pl-c1">0</span>
<span class="pl-s1">pytz</span>: <span class="pl-c1">2016.1</span>
<span class="pl-s1">blosc</span>: <span class="pl-c1">None</span>
<span class="pl-s1">bottleneck</span>: <span class="pl-c1">1.0</span>.<span class="pl-c1">0</span>
<span class="pl-s1">tables</span>: <span class="pl-c1">3.2</span>.<span class="pl-c1">2</span>
<span class="pl-s1">numexpr</span>: <span class="pl-c1">2.4</span>.<span class="pl-c1">6</span>
<span class="pl-s1">matplotlib</span>: <span class="pl-c1">1.5</span>.<span class="pl-c1">1</span>
<span class="pl-s1">openpyxl</span>: <span class="pl-c1">2.3</span>.<span class="pl-c1">2</span>
<span class="pl-s1">xlrd</span>: <span class="pl-c1">0.9</span>.<span class="pl-c1">4</span>
<span class="pl-s1">xlwt</span>: <span class="pl-c1">1.0</span>.<span class="pl-c1">0</span>
<span class="pl-s1">xlsxwriter</span>: <span class="pl-c1">0.8</span>.<span class="pl-c1">4</span>
<span class="pl-s1">lxml</span>: <span class="pl-c1">3.6</span>.<span class="pl-c1">0</span>
<span class="pl-s1">bs4</span>: <span class="pl-c1">4.4</span>.<span class="pl-c1">1</span>
<span class="pl-s1">html5lib</span>: <span class="pl-c1">None</span>
<span class="pl-s1">httplib2</span>: <span class="pl-c1">0.9</span>.<span class="pl-c1">2</span>
<span class="pl-s1">apiclient</span>: <span class="pl-c1">1.5</span>.<span class="pl-c1">0</span>
<span class="pl-s1">sqlalchemy</span>: <span class="pl-c1">1.0</span>.<span class="pl-c1">12</span>
<span class="pl-s1">pymysql</span>: <span class="pl-c1">None</span>
<span class="pl-s1">psycopg2</span>: <span class="pl-c1">None</span>
<span class="pl-s1">jinja2</span>: <span class="pl-c1">2.8</span>
<span class="pl-s1">boto</span>: <span class="pl-c1">2.39</span>.<span class="pl-c1">0</span></pre></div>