在带有日期索引的 Pandas 中删除一行,python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11414596/
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-13 15:46:15  来源:igfitidea点击:

dropping a row in pandas with dates indexes, python

pythonpandas

提问by user1504276

I'm trying to drop the last row in a dataframecreated by pandas in python and seem to be having trouble.

我试图将最后一行放在dataframe由 python 中的熊猫创建的a中,似乎遇到了问题。

index = DateRange('1/1/2000', periods=8)
df = DataFrame(randn(8, 3), index=index, columns=['A', 'B', 'C'])

I tried the drop method like this:

我尝试了这样的 drop 方法:

df.drop([shape(df)[0]-1], axis = 0)

but it keeps saying label not contained in the axis.

但它一直说标签不包含在轴中。

I also tried to drop by index name and it still doesn't seem to be working.

我还尝试按索引名称删除,但它似乎仍然不起作用。

Any advice would be appreciated. Thanks!!!

任何意见,将不胜感激。谢谢!!!

回答by eumiro

df.ix[:-1]

returns the original DataFrame with the last row removed.

返回删除最后一行的原始数据帧。

回答by modpy

Referencing the DataFrame directly to retrieve all but the last index worked for me.

直接引用 DataFrame 以检索除最后一个索引之外的所有内容对我有用。

df[:-1]