consider the pd.Series
s
JavaScript
x
15
15
1
s = pd.Series(list('abcdefghij'), list('ABCDEFGHIJ'))
2
s
3
4
A a
5
B b
6
C c
7
D d
8
E e
9
F f
10
G g
11
H h
12
I i
13
J j
14
dtype: object
15
What is the quickest way to swap index and values and get the following
JavaScript
1
12
12
1
a A
2
b B
3
c C
4
d D
5
e E
6
f F
7
g G
8
h H
9
i I
10
j J
11
dtype: object
12
Advertisement
Answer
One posible solution is swap keys and values by:
JavaScript
1
14
14
1
s1 = pd.Series(dict((v,k) for k,v in s.iteritems()))
2
print (s1)
3
a A
4
b B
5
c C
6
d D
7
e E
8
f F
9
g G
10
h H
11
i I
12
j J
13
dtype: object
14
Another the fastest:
JavaScript
1
13
13
1
print (pd.Series(s.index.values, index=s ))
2
a A
3
b B
4
c C
5
d D
6
e E
7
f F
8
g G
9
h H
10
i I
11
j J
12
dtype: object
13
Timings:
JavaScript
1
8
1
In [63]: %timeit pd.Series(dict((v,k) for k,v in s.iteritems()))
2
The slowest run took 6.55 times longer than the fastest. This could mean that an intermediate result is being cached.
3
10000 loops, best of 3: 146 µs per loop
4
5
In [71]: %timeit (pd.Series(s.index.values, index=s ))
6
The slowest run took 7.42 times longer than the fastest. This could mean that an intermediate result is being cached.
7
10000 loops, best of 3: 102 µs per loop
8
If length of Series
is 1M
:
JavaScript
1
16
16
1
s = pd.Series(list('abcdefghij'), list('ABCDEFGHIJ'))
2
s = pd.concat([s]*1000000).reset_index(drop=True)
3
print (s)
4
5
In [72]: %timeit (pd.Series(s.index, index=s ))
6
10000 loops, best of 3: 106 µs per loop
7
8
In [229]: %timeit pd.Series(dict((v,k) for k,v in s.iteritems()))
9
1 loop, best of 3: 1.77 s per loop
10
11
In [230]: %timeit (pd.Series(s.index, index=s ))
12
10 loops, best of 3: 130 ms per loop
13
14
In [231]: %timeit (pd.Series(s.index.values, index=s ))
15
10 loops, best of 3: 26.5 ms per loop
16