From 42c8cc1fd21582a9d80b63642cb4ceb2434c74b3 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Sat, 14 Nov 2020 19:20:31 +0100 Subject: [PATCH 1/3] DOC: section in indexing user guide to show use of np.where --- doc/source/user_guide/indexing.rst | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index 98c981539d207..49b4b35e0c55d 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -1158,6 +1158,39 @@ Mask s.mask(s >= 0) df.mask(df >= 0) +.. _indexing.np_where: + +Setting with enlargement conditionally using :func:`numpy` +---------------------------------------------------------- + +An alternative to :meth:`~pandas.DataFrame.where` is to use :func:`numpy.where`. +Combined with setting a new column, you can use it to enlarge a dataframe where the +values are determined conditionally. + +When you have two choices to choose from, say in the following dataframe, set the color +to 'green' when the second column has 'Z'. You can do the following: + +.. ipython:: python + + df = pd.DataFrame({'col1':list('ABBC'), 'col2':list('ZZXY')}) + df['color'] = np.where(df['col2'] == 'Z', 'green', 'red') + df + +If you have more than one conditions, you can use :func:`numpy.select` to achieve that. +Say corresponding to three conditions there are three choice of colors, with a fourth +color as a fallback, you can do the following. + +.. ipython:: python + + conditions = [ + (df['col2'] == 'Z') & (df['col1'] == 'A'), + (df['col2'] == 'Z') & (df['col1'] == 'B'), + (df['col1'] == 'B') + ] + choices = ['yellow', 'blue', 'purple'] + df['color'] = np.select(conditions, choices, default='black') + df + .. _indexing.query: The :meth:`~pandas.DataFrame.query` Method From d47ebd15e2687744cf99a14b0820b99864c603ae Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Sat, 14 Nov 2020 19:40:07 +0100 Subject: [PATCH 2/3] Add missing whitespace for style --- doc/source/user_guide/indexing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index 49b4b35e0c55d..da3f90965bdf0 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -1172,7 +1172,7 @@ to 'green' when the second column has 'Z'. You can do the following: .. ipython:: python - df = pd.DataFrame({'col1':list('ABBC'), 'col2':list('ZZXY')}) + df = pd.DataFrame({'col1': list('ABBC'), 'col2': list('ZZXY')}) df['color'] = np.where(df['col2'] == 'Z', 'green', 'red') df From 67ac0a0330fa5f689d123cf493c2d378664178f3 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Sat, 14 Nov 2020 19:58:25 +0100 Subject: [PATCH 3/3] Simplify phrasing --- doc/source/user_guide/indexing.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index da3f90965bdf0..da0b3a12f44b6 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -1167,8 +1167,9 @@ An alternative to :meth:`~pandas.DataFrame.where` is to use :func:`numpy.where`. Combined with setting a new column, you can use it to enlarge a dataframe where the values are determined conditionally. -When you have two choices to choose from, say in the following dataframe, set the color -to 'green' when the second column has 'Z'. You can do the following: +Consider you have two choices to choose from in the following dataframe. And you want to +set a new column color to 'green' when the second column has 'Z'. You can do the +following: .. ipython:: python @@ -1176,9 +1177,9 @@ to 'green' when the second column has 'Z'. You can do the following: df['color'] = np.where(df['col2'] == 'Z', 'green', 'red') df -If you have more than one conditions, you can use :func:`numpy.select` to achieve that. -Say corresponding to three conditions there are three choice of colors, with a fourth -color as a fallback, you can do the following. +If you have multiple conditions, you can use :func:`numpy.select` to achieve that. Say +corresponding to three conditions there are three choice of colors, with a fourth color +as a fallback, you can do the following. .. ipython:: python