Skip to content

Commit cdc235f

Browse files
Backport PR pandas-dev#42110: DOC-Modified documentation for the issue GH42106 (pandas-dev#42161)
Co-authored-by: Abhishek R <[email protected]>
1 parent 0205ddd commit cdc235f

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

doc/source/user_guide/indexing.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -1523,18 +1523,17 @@ Looking up values by index/column labels
15231523
----------------------------------------
15241524

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

15291529
.. ipython:: python
15301530
15311531
df = pd.DataFrame({'col': ["A", "A", "B", "B"],
15321532
'A': [80, 23, np.nan, 22],
15331533
'B': [80, 55, 76, 67]})
15341534
df
1535-
melt = df.melt('col')
1536-
melt = melt.loc[melt['col'] == melt['variable'], 'value']
1537-
melt.reset_index(drop=True)
1535+
idx, cols = pd.factorize(df['col'])
1536+
df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx]
15381537
15391538
Formerly this could be achieved with the dedicated ``DataFrame.lookup`` method
15401539
which was deprecated in version 1.2.0.

0 commit comments

Comments
 (0)