简体   繁体   English

python pandas series获取一行的索引号

[英]python pandas series get index number of a row

I have a series that looks like this 我有一个看起来像这样的系列

2014  7   2014-07-01   -0.045417
      8   2014-08-01   -0.035876
      9   2014-09-02   -0.030971
      10  2014-10-01   -0.027471
      11  2014-11-03   -0.032968
      12  2014-12-01   -0.031110
2015  1   2015-01-02   -0.028906
      2   2015-02-02   -0.035563
      3   2015-03-02   -0.040338
      4   2015-04-01   -0.032770
      5   2015-05-01   -0.025762
      6   2015-06-01   -0.019746
      7   2015-07-01   -0.018541
      8   2015-08-03   -0.028101
      9   2015-09-01   -0.043237
      10  2015-10-01   -0.053565
      11  2015-11-02   -0.062630
      12  2015-12-01   -0.064618
2016  1   2016-01-04   -0.064852

I want to be able to get the index value of a row from a date. 我希望能够从日期获取行的索引值。 Something like: 就像是:

myseries.loc(axis=0)[:,:,'2015-10-01']. and it returns 15 它返回15

Use .get_loc and pass the index: 使用.get_loc并传递索引:

In [52]:
s.index.get_loc(s.loc[:,:,'2015-10-01'].index[0])

Out[52]:
15

You have to get the specific index tuple value here which is accessed by indexing the first index value to return the tuple: 你必须在这里得到特定的索引元组值,通过索引第一个索引值来返回元组来访问它:

In [54]:
s.loc[:,:,'2015-10-01'].index[0]

Out[54]:
(2015, 10, '2015-10-01')

If you didn't do this you get all this: 如果你没有这样做,你会得到这一切:

In [55]:
s.loc[:,:,'2015-10-01'].index

Out[55]:
MultiIndex(levels=[[2014, 2015, 2016], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], ['2014-07-01', '2014-08-01', '2014-09-02', '2014-10-01', '2014-11-03', '2014-12-01', '2015-01-02', '2015-02-02', '2015-03-02', '2015-04-01', '2015-05-01', '2015-06-01', '2015-07-01', '2015-08-03', '2015-09-01', '2015-10-01', '2015-11-02', '2015-12-01', '2016-01-04']],
           labels=[[1], [9], [15]],
           names=['year', 'month', 'date'])

Which you can't pass to .get_loc 你无法传递给.get_loc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM