按索引从Pandas系列中删除元素 [英] Drop Elements from Pandas Series by Index
本文介绍了按索引从Pandas系列中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫系列df(日期=索引):
I have a pandas series df (dates = index):
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
我想将日期从2015-09-11拖放到2015-09-15,所以我的df如下所示:
I'd like to drop the dates from 2015-09-11 to 2015-09-15, so my df would look like:
2015-09-10 58
2015-09-16 99
2015-09-17 124
我尝试使用df.drop ["2015-09-11":"2015-09-15"],但出现错误:
I've tried using df.drop["2015-09-11":"2015-09-15"], but i get an error:
TypeError: 'instancemethod' object has no attribute '__getitem__'
有什么建议吗?
谢谢!
推荐答案
尝试一下:
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
这篇关于按索引从Pandas系列中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文