how to convert pandas series to tuple of index and value
Well it seems simply zip(s,s.index)
works too!
For Python-3.x, we need to wrap it with list
-
list(zip(s,s.index))
To get a tuple of tuples, use tuple()
: tuple(zip(s,s.index))
.
Sample run -
In [8]: sOut[8]: a 1b 2c 3dtype: int64In [9]: list(zip(s,s.index))Out[9]: [(1, 'a'), (2, 'b'), (3, 'c')]In [10]: tuple(zip(s,s.index))Out[10]: ((1, 'a'), (2, 'b'), (3, 'c'))
One possibility is to swap the order of the index elements and the values from iteritems
:
res = [(val, idx) for idx, val in s.iteritems()]
EDIT: @Divakar's answer is faster by about a factor of 2. Building a seriesĀ of random strings for testing:
N = 100000str_len = 4ints = range(N)strs = [None]*Nfor i in ints: strs[i] = ''.join(random.choice(string.ascii_letters) for _ in range(str_len))s = pd.Series(ints, strs)
Timings:
%timeit res = zip(s,s.index)>>> 100 loops, best of 3: 14.8 ms per loop%timeit res = [(val, idx) for idx, val in s.iteritems()]>>> 10 loops, best of 3: 26.7 ms per loop