按指数从熊猫系列中删除元素

时间:2015-12-03 12:49:41

标签: python pandas dataframe

我有一个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

我想删除2015-09-11至2015-09-15的日期,所以我的df看起来像:

2015-09-10     58
2015-09-16     99
2015-09-17    124

我尝试过使用df.drop [“2015-09-11”:“2015-09-15”],但是我收到了一个错误:

TypeError: 'instancemethod' object has no attribute '__getitem__'

有任何建议吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

试试:

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