python pandas系列来自多索引的loc值

时间:2016-01-22 14:24:59

标签: python pandas

我有一个看起来像这样的系列

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

我希望能够从日期中获取价值。类似的东西:

myseries.loc('2015-10-01')并返回-0.053565

索引是(2016, 1, 2016-01-04)

形式的元组

2 个答案:

答案 0 :(得分:3)

你可以这样做:

+--- com.android.support:multidex:1.0.1
+--- com.android.support:support-v4:21.0.3 -> 22.2.0
|    \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:appcompat-v7:21.0.3
|    \--- com.android.support:support-v4:21.0.3 -> 22.2.0 (*)
+--- com.android.support:multidex:1.0.0 -> 1.0.1
\--- com.google.android.gms:play-services-gcm:7.+ -> 7.8.0
     \--- com.google.android.gms:play-services-base:7.8.0
          \--- com.android.support:support-v4:22.2.0 (*)

您还可以为每个级别传递slice

In [32]:
df.loc(axis=0)[:,:,'2015-10-01']

Out[32]:
                          value
year month date                
2015 10    2015-10-01 -0.053565

或者只是传递前2个索引级别:

In [39]:
df.loc[(slice(None),slice(None),'2015-10-01'),]

Out[39]:
                          value
year month date                
2015 10    2015-10-01 -0.053565|

答案 1 :(得分:3)

尝试xs

print s.xs('2015-10-01',level=2,axis=0)
#year  datetime 
#2015  10   -0.053565
#Name: series, dtype: float64

print s.xs(7,level=1,axis=0)
#year  datetime  
#2014  2014-07-01   -0.045417
#2015  2015-07-01   -0.018541
#Name: series, dtype: float64