从系列中获取索引和值

时间:2014-01-17 15:06:13

标签: python indexing pandas append series

我在pandas中选择和索引时有点慢。

我有一个日期时间系列,我试图从中选择某些元素及其日期时间索引,以便将它们附加到新系列。 例如:

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]

给出输出:

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

我想要输出

1  11
2  12
3  13
4  14
5  15
etc

无论如何也要获得索引? (我不只是想打印它,我想在一个新系列中附加一些。例如,将所有可被2整除的数字加到一个系列中,连同它们的索引)。

很抱歉,如果这很明显,那就是漫长的一天。

2 个答案:

答案 0 :(得分:2)

要同时获取索引和值,您可以迭代序列。请注意,默认索引从0开始,而不是从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

当然,您可以为系列分配自定义索引:

>>> 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

不要完全理解“我想将一些内容添加到新系列”,但您可以使用index属性访问索引:

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

或将其移动到系列中,制作数据框:

>>> 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]

答案 1 :(得分:1)

您可以使用enumerate同时获取索引以及项目:

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   

要获得新系列,请使用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版本:

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