按索引删除Pandas系列中的元素

2024-04-25 17:11:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫系列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年9月11日推迟到2015年9月15日,所以我的df应该是:

^{pr2}$

我试过用测向下降[“2015-09-11”:“2015-09-15”],但我得到一个错误:

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

有什么建议吗?在

谢谢!在


Tags: nodfobject错误attribute建议hasgetitem
1条回答
网友
1楼 · 发布于 2024-04-25 17:11:20

试试看:

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

相关问题 更多 >