按索引从 Pandas 系列中删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34066533/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:19:05 来源:igfitidea点击:
Drop Elements from Pandas Series by Index
提问by jjjayn
I have a pandas series df (dates = index):
我有一个Pandas系列 df(日期 = 索引):
2015-09-10 58
2015-09-11 40
2015-09-12 33
2015-09-13 42
2015-09-14 22
2015-09-15 88
2015-09-16 99
2015-09-17 124
I'd like to drop the dates from 2015-09-11 to 2015-09-15, so my df would look like:
我想将日期从 2015-09-11 删除到 2015-09-15,所以我的 df 看起来像:
2015-09-10 58
2015-09-16 99
2015-09-17 124
I've tried using df.drop["2015-09-11":"2015-09-15"], but i get an error:
我试过使用 df.drop["2015-09-11":"2015-09-15"],但出现错误:
TypeError: 'instancemethod' object has no attribute '__getitem__'
Any advices?
有什么建议吗?
Thanks!
谢谢!
回答by Anton Protopopov
Try that:
试试看:
s = pd.Series([58,40,33,42,22,88,99,124], index =["2015-09-10","2015-09-11","2015-09-12","2015-09-13","2015-09-14","2015-09-15","2015-09-16","2015-09-17"])
In [140]: s
Out[140]:
2015-09-10 58
2015-09-11 40
2015-09-12 33
2015-09-13 42
2015-09-14 22
2015-09-15 88
2015-09-16 99
2015-09-17 124
dtype: int64
s.drop(s["2015-09-11":"2015-09-15"].index)
In [142]: s.drop(s["2015-09-11":"2015-09-15"].index)
Out[142]:
2015-09-10 58
2015-09-16 99
2015-09-17 124
dtype: int64