Подтвердить что ты не робот

Самый быстрый способ обмена индексом со значениями

рассмотрим 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

Каков самый быстрый способ обмена индексом и значениями и получить следующие

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

Ответ 1

Одним из возможных решений являются клавиши и значения swap:

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

Другой самый быстрый:

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

Задержка

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

Если длина Series равна 1M:

s = pd.Series(list('abcdefghij'), list('ABCDEFGHIJ'))
s = pd.concat([s]*1000000).reset_index(drop=True)
print (s)

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

In [229]: %timeit pd.Series(dict((v,k) for k,v in s.iteritems()))
1 loop, best of 3: 1.77 s per loop

In [230]: %timeit (pd.Series(s.index, index=s ))
10 loops, best of 3: 130 ms per loop

In [231]: %timeit (pd.Series(s.index.values, index=s ))
10 loops, best of 3: 26.5 ms per loop