Python 从 Pandas 系列中删除零行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32067054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove rows of zeros from a Pandas series
提问by BLL27
I have a number Pandas
Series with 601 rows indexed by date as seen below. The values are zero up until a point, after which all the values are non zero. This point varies with each Series but I would like a way to remove all the rows where the value is zero while keeping the integrity of the date index.
我有一个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
采纳答案by EdChum
Just filter them out:
只需过滤掉它们:
users[users!=0]
This will preserve your index also
这也将保留您的索引
Or
或者
users[users > 0]
if it's positive values you're after:
如果你追求的是正值:
In [38]:
s[s>0]
Out[38]:
2015-08-17 18:29:59 18
2015-08-16 18:29:59 3
2015-08-15 18:29:59 11
2015-08-14 18:29:59 12
2015-08-13 18:29:59 8
2015-08-12 18:29:59 10
2015-08-11 18:29:59 6
2015-08-10 18:29:59 6
2015-08-09 18:29:59 7
2015-08-08 18:29:59 7
2015-08-07 18:29:59 13
2015-08-06 18:29:59 16
2015-08-05 18:29:59 12
2015-08-04 18:29:59 14
2015-08-03 18:29:59 5
2015-08-02 18:29:59 5
2015-08-01 18:29:59 8
2015-07-31 18:29:59 6
2015-07-30 18:29:59 7
2015-07-29 18:29:59 9
2015-07-28 18:29:59 7
2015-07-27 18:29:59 5
2015-07-26 18:29:59 4
2015-07-25 18:29:59 8
2015-07-24 18:29:59 8
2015-07-23 18:29:59 8
2015-07-22 18:29:59 9
2015-07-21 18:29:59 5
2015-07-20 18:29:59 7
2015-07-19 18:29:59 6
Name: 1, dtype: int64
回答by Uri Goren
if ds
is you DataSeries
: ds!=0
will return a boolean vector of rows with values different than zero.
如果ds
是你DataSeries
:ds!=0
将返回值不为零的行的布尔向量。
ds[ds!=0]
are the rows, with the index preserved
ds[ds!=0]
是行,保留索引
Note that missing values (NaN
) will not be filtered.
请注意,NaN
不会过滤缺失值 ( )。
To filter both, use: ds[(ds!=0)&(pd.isnull(ds))]
要过滤两者,请使用: ds[(ds!=0)&(pd.isnull(ds))]