Skip to content

Stratified sampling #33762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Version 1.0
.. toctree::
:maxdepth: 2

v1.0.4
v1.0.3
v1.0.2
v1.0.1
Expand Down
69 changes: 69 additions & 0 deletions doc/source/whatsnew/v1.0.4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.. _whatsnew_104:

What's new in 1.0.4 (??)
------------------------

These are the changes in pandas 1.0.4. See :ref:`release` for a full changelog
including other versions of pandas.


.. ---------------------------------------------------------------------------

Enhancements
~~~~~~~~~~~~

.. _whatsnew_104.stratified_sample:

Stratified Sampling in Pandas
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is a new feature, it would go in 1.1


We've added :meth:`~DataFrame.stratified_sample` for sampling a DataFrame object using strata.

A stratified sample is used when it is desired to keep the same proportions of the variables in the original population when sampling.

For example:

.. code-block:: ipython

In [1]: df = pd.DataFrame({'gender': ['male', 'male', 'female', 'female', 'female',
... 'female', 'female', 'female', 'male', 'male'],
... 'age': [25, 26, 25, 26, 30, 25, 25, 30, 30, 25],
... 'country': ['US', 'CAN', 'MEX', 'CAN', 'IN', 'CAN',
... 'CAN', 'US', 'CAN', 'IN'],
... 'income_K' : [100, 110, 99, 110, 110, 100, 100, 110,
... 100, 99]}
)
In [2]: df.stratified_sample(strata=['age'], n=5, random_state=0, reset_index=True)
Out[2]:
gender age country income_K
0 female 25 CAN 100
1 male 25 US 100
2 female 26 CAN 110
3 male 30 CAN 100
4 female 30 US 110

We've also added :meth:`~DataFrame.stratified_sample_counts` to return the counts that would be generated by a stratified sampling.

.. code-block:: ipython

In [1]: df = pd.DataFrame({'gender': ['male', 'male', 'female', 'female', 'female',
... 'female', 'female', 'female', 'male', 'male'],
... 'age': [25, 26, 25, 26, 30, 25, 25, 30, 30, 25],
... 'country': ['US', 'CAN', 'MEX', 'CAN', 'IN', 'CAN',
... 'CAN', 'US', 'CAN', 'IN'],
... 'income_K' : [100, 110, 99, 110, 110, 100, 100, 110,
... 100, 99]}
)
In [2]: df.stratified_sample_counts(strata=['age'], n=5)
Out[2]:
age size sample_size
0 25 5 2
1 26 2 1
2 30 3 2


.. _whatsnew_104.contributors:

Contributors
~~~~~~~~~~~~
* Flavio Bossolan +
Loading