2015-08-19 94 views
2

我有一個數字Pandas系列,其中601行按日期進行索引,如下所示。直到引入一些非零值時,值爲零。這一點隨每個系列而不同,因此我想找到一種方法來查找第一個非零值的索引並從該點開始繪製。在熊貓系列中找到第一個非零值

Name: users, dtype: float64 dates 
2015-08-17 14:29:59-04:00 18 
2015-08-16 14:29:59-04:00  3 
2015-08-15 14:29:59-04:00 11 
2015-08-14 14:29:59-04:00 12 
2015-08-13 14:29:59-04:00  8 
2015-08-12 14:29:59-04:00 10 
2015-08-11 14:29:59-04:00  6 
2015-08-10 14:29:59-04:00  6 
2015-08-09 14:29:59-04:00  7 
2015-08-08 14:29:59-04:00  7 
2015-08-07 14:29:59-04:00 13 
2015-08-06 14:29:59-04:00 16 
2015-08-05 14:29:59-04:00 12 
2015-08-04 14:29:59-04:00 14 
2015-08-03 14:29:59-04:00  5 
2015-08-02 14:29:59-04:00  5 
2015-08-01 14:29:59-04:00  8 
2015-07-31 14:29:59-04:00  6 
2015-07-30 14:29:59-04:00  7 
2015-07-29 14:29:59-04:00  9 
2015-07-28 14:29:59-04:00  7 
2015-07-27 14:29:59-04:00  5 
2015-07-26 14:29:59-04:00  4 
2015-07-25 14:29:59-04:00  8 
2015-07-24 14:29:59-04:00  8 
2015-07-23 14:29:59-04:00  8 
2015-07-22 14:29:59-04:00  9 
2015-07-21 14:29:59-04:00  5 
2015-07-20 14:29:59-04:00  7 
2015-07-19 14:29:59-04:00  6 
          .. 
2014-01-23 13:29:59-05:00  0 
2014-01-22 13:29:59-05:00  0 
2014-01-21 13:29:59-05:00  0 
2014-01-20 13:29:59-05:00  0 
2014-01-19 13:29:59-05:00  0 
2014-01-18 13:29:59-05:00  0 
2014-01-17 13:29:59-05:00  0 
2014-01-16 13:29:59-05:00  0 
2014-01-15 13:29:59-05:00  0 
2014-01-14 13:29:59-05:00  0 
2014-01-13 13:29:59-05:00  0 
2014-01-12 13:29:59-05:00  0 
2014-01-11 13:29:59-05:00  0 
2014-01-10 13:29:59-05:00  0 
2014-01-09 13:29:59-05:00  0 
2014-01-08 13:29:59-05:00  0 
2014-01-07 13:29:59-05:00  0 
2014-01-06 13:29:59-05:00  0 
2014-01-05 13:29:59-05:00  0 
2014-01-04 13:29:59-05:00  0 
2014-01-03 13:29:59-05:00  0 
2014-01-02 13:29:59-05:00  0 
2014-01-01 13:29:59-05:00  0 
2013-12-31 13:29:59-05:00  0 
2013-12-30 13:29:59-05:00  0 
2013-12-29 13:29:59-05:00  0 
2013-12-28 13:29:59-05:00  0 
2013-12-27 13:29:59-05:00  0 
2013-12-26 13:29:59-05:00  0 
2013-12-25 13:29:59-05:00  0 

回答

5

假設您的系列被命名爲s

s = s.sort_index() 
start = s.loc[s != 0].index 
if len(start) > 0: 
    filtered_series = s.ix[start[0]:] 

你的數據似乎在時間向後排列(即最近的第一個),所以我第一次排序的指標。

然後我用loc來獲得系列中非負值的索引值。如果這個列表返回任何東西(它可能是一個空列表),那麼我使用.ix從上面計算的第一個非零指數值到系列末尾的索引值。