user2761786
user2761786

Reputation: 271

Getting the index and value from a Series

I'm having a bit of a slow moment with selection and indexing in pandas.

I have a Date Time series from which I am trying to select certain elements, along with their date time index in order to append them to a new series. Example:

import pandas as pd

x=pd.Series([11,12,13,14,15,16,17,18,19,20])
for i in range(len(x)):
    print x.ix[i]

Gives the output:

runfile('C:/Users/AClayton/WinPython-64bit-2.7.5.3/python-2.7.5.amd64/untitled6.py', wdir='C:/Users/AClayton/WinPython-64bit-2.7.5.3/python-2.7.5.amd64')
11
12
13
14
15
16
17
18
19
20

I would like to have the output

1  11
2  12
3  13
4  14
5  15
etc

Anyway to get the index too? (I don't just want to print it, I want to append some to a new series. For example, add all the numbers divisible by two to a Series, along with their index).

Sorry if this is obvious, been a long day.

Upvotes: 4

Views: 6857

Answers (2)

alko
alko

Reputation: 48317

To get both index and value, you can iterate over series. Note that default index starts from 0, not from 1:

>>> for i, v in x.iteritems():
...    print i, v
...
0 11
1 12
2 13
3 14
4 15
5 16
6 17
7 18
8 19
9 20

Sure you can assign a custom index to a series:

>>> x.index = range(1, 6)*2
>>> for i, v in x.iteritems():
...    print i, v
...
1 11
2 12
3 13
4 14
5 15
1 16
2 17
3 18
4 19
5 20

Don't completely get what you mean with "I want to append some to a new series", but you can access index with index property:

>>> x.index
Int64Index([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], dtype='int64')

or move it into series, making a dataframe:

>>> x.reset_index()
   index   0
0      1  11
1      2  12
2      3  13
3      4  14
4      5  15
5      1  16
6      2  17
7      3  18
8      4  19
9      5  20

[10 rows x 2 columns]

Upvotes: 4

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

You can use enumerate to get both index as well as the item:

In [4]: import pandas as pd                                                                      

In [5]: x = pd.Series([11,12,13,14,15,16,17,18,19,20])

In [6]: for ind, item in enumerate(x, 1):                                                        
    print ind, item                                                                              
   ...: 
1 11                                                                                             
2 12                                                                                             
3 13                                                                                             
4 14                                                                                             
5 15                                                                                             
6 16                                                                                             
7 17                                                                                             
8 18                                                                                             
9 19                                                                                             
10 20   

To get a new series use a list comprehension inside pandas.Series:

In [25]: indices = [i for i, v in enumerate(x, 1) if v%2]                                        

In [26]: s = pd.Series([(i, x[i-1]) for i in indices], index=indices)            

In [27]: s
Out[27]: 
1    (1, 11)                                                                                     
3    (3, 13)                                                                                     
5    (5, 15)                                                                                     
7    (7, 17)                                                                                     
9    (9, 19)                                                                                     
dtype: object                                                                                    

Numpy version:

In [53]: import numpy as np

In [54]: indices = np.where(x%2)[0] + 1                                                          

In [55]: pd.Series(np.column_stack((indices, x[indices-1])).tolist(), index=indices)             
Out[55]: 
1    [1, 11]                                                                                     
3    [3, 13]                                                                                     
5    [5, 15]                                                                                     
7    [7, 17]                                                                                     
9    [9, 19]                                                                                     
dtype: object   

Upvotes: 1

Related Questions