Skip to content

Equivalent of Series.map for DataFrame #12550

@BrenBarn

Description

@BrenBarn

I think there should be a way to take a DataFrame and use the values in its columns as indices into a MultiIndexed Series. Here is an example:

>>> d = pandas.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]})
>>> d
  Let  Num
0   A    1
1   B    2
2   C    3
>>> ser = pandas.Series(
...     ['a', 'b', 'c', 'd', 'e', 'f'],
...     index=pandas.MultiIndex.from_arrays([["A", "B", "C"]*2, [1, 2, 3, 4, 5, 6]])
... )
>>> ser
A  1    a
B  2    b
C  3    c
A  4    d
B  5    e
C  6    f
dtype: object

With this data, you should be able to do d.map(ser) (or whatever method name instead of map) and get the same result as this:

>>> ser.ix[d.apply(tuple, axis=1)]
A  1    a
B  2    b
C  3    c
dtype: object

You currently cannot do this without converting the rows to tuples (ser[d] gives ValueError: Cannot index with multidimensional key). Converting to tuple is an awkward way to do a very natural task, which is using a tabular array of data to look up values in a tabular index (i.e., a MultiIndex).

I'm creating an issue to resurrect this request from much earlier discussion here and here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions