You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In previous versions of pandas, :meth:`DataFrame.value_counts` with ``sort=False`` would sort the result by row labels (as was documented). This was nonintuitive and inconsistent with :meth:`Series.value_counts` which would maintain the order of the input. Now :meth:`DataFrame.value_counts` will maintain the order of the input.
212
+
213
+
.. ipython:: python
214
+
215
+
df = pd.DataFrame(
216
+
{
217
+
"a": [2, 2, 2, 2, 1, 1, 1, 1],
218
+
"b": [2, 1, 3, 1, 2, 3, 1, 1],
219
+
}
220
+
)
221
+
df
222
+
223
+
*Old behavior*
224
+
225
+
.. code-block:: ipython
226
+
227
+
In [3]: df.value_counts(sort=False)
228
+
Out[3]:
229
+
a b
230
+
1 1 2
231
+
2 1
232
+
3 1
233
+
2 1 2
234
+
2 1
235
+
3 1
236
+
Name: count, dtype: int64
237
+
238
+
*New behavior*
239
+
240
+
.. ipython:: python
241
+
242
+
df.value_counts(sort=False)
243
+
244
+
This change also applies to :meth:`.DataFrameGroupBy.value_counts`. Here, there are two options for sorting: one ``sort`` passed to :meth:`DataFrame.groupby` and one passed directly to :meth:`.DataFrameGroupBy.value_counts`. The former will determine whether to sort the groups, the latter whether to sort the counts. All non-grouping columns will maintain the order of the input *within groups*.
245
+
246
+
*Old behavior*
247
+
248
+
.. code-block:: ipython
249
+
250
+
In [5]: df.groupby("a", sort=True).value_counts(sort=False)
0 commit comments