【问题标题】:Getting the index and value from a Series从系列中获取索引和值
【发布时间】:2014-02-06 23:20:54
【问题描述】:

我在 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 整除的数字连同它们的索引一起添加到一个系列中)。

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

【问题讨论】:

  • 如果速度很重要,你应该避免在 pandas 对象上使用 for 循环。您使用什么标准过滤到最后一个? x <= 15 之类的内容可能正是您想要的。
  • 我有一台产生一定功率的机器。它有两种状态,一种是通常的稳定状态,另一种是低于 400。所以我会使用 x
  • 查看索引文档,尤其是boolean indexing。如果您有任何问题,最好发布新问题。

标签: python indexing pandas append series


【解决方案1】:

要同时获取索引和值,您可以遍历系列。请注意,默认索引从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]

【讨论】:

    【解决方案2】:

    您可以使用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   
    

    【讨论】:

    • 非常感谢,看列表推导,有没有办法保持原索引为索引?我必须使用reindex吗?
    • @user2761786 查看更新后的解决方案,如果可以使用numpy则:indices = np.where(x%2)[0] + 1
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2014-03-07
    • 2023-03-17
    • 2012-08-30
    • 2012-08-03
    相关资源
    最近更新 更多