Skip to content

Backport PR #42110 on branch 1.3.x (DOC-Modified documentation for the issue GH42106) #42161

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

Merged
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
9 changes: 4 additions & 5 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1523,18 +1523,17 @@ Looking up values by index/column labels
----------------------------------------

Sometimes you want to extract a set of values given a sequence of row labels
and column labels, this can be achieved by ``DataFrame.melt`` combined by filtering the corresponding
rows with ``DataFrame.loc``. For instance:
and column labels, this can be achieved by ``pandas.factorize`` and NumPy indexing.
For instance:

.. ipython:: python

df = pd.DataFrame({'col': ["A", "A", "B", "B"],
'A': [80, 23, np.nan, 22],
'B': [80, 55, 76, 67]})
df
melt = df.melt('col')
melt = melt.loc[melt['col'] == melt['variable'], 'value']
melt.reset_index(drop=True)
idx, cols = pd.factorize(df['col'])
df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx]

Formerly this could be achieved with the dedicated ``DataFrame.lookup`` method
which was deprecated in version 1.2.0.
Expand Down