BlindDriver BlindDriver - 1 month ago 7
Python Question

functional composition of pandas' Series

What is the best way to get functional composition of pandas

Series
? To clarify the question, a
Series
is a map from its index to its values. Given two Series
s
and
t
how do I construct their composition
u
such that its index is that of s and for each
i
in
s.index
u[i] = t[s[i]]
. I currently do the following:

u = pd.Series(t[s].values, index=s.index)


Is there a more elegant way?

Answer

That's what map is for. Just do u = s.map(t).