piRSquared piRSquared - 1 month ago 15
Python Question

quickest way to swap index with values

consider the

pd.Series
s


s = pd.Series(list('abcdefghij'), list('ABCDEFGHIJ'))
s

A a
B b
C c
D d
E e
F f
G g
H h
I i
J j
dtype: object


What is the quickest way to swap index and values and get the following

a A
b B
c C
d D
e E
f F
g G
h H
i I
j J
dtype: object

Answer

One posible solution is swap keys and values by:

s1 = pd.Series(dict((v,k) for k,v in s.iteritems()))
print (s1)
a    A
b    B
c    C
d    D
e    E
f    F
g    G
h    H
i    I
j    J
dtype: object

Another one:

print (pd.Series(s.index, index=s ))
a    A
b    B
c    C
d    D
e    E
f    F
g    G
h    H
i    I
j    J
dtype: object

Timings:

In [63]: %timeit pd.Series(dict((v,k) for k,v in s.iteritems()))
The slowest run took 6.55 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 146 µs per loop

In [71]: %timeit (pd.Series(s.index.values, index=s ))
The slowest run took 7.42 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 102 µs per loop

In [72]: %timeit (pd.Series(s.index, index=s ))
10000 loops, best of 3: 106 µs per loop
Comments