Pandas DataFrame 索引的自动递增选项

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

Autoincrementing option for Pandas DataFrame index

pythonindexingappendrowpandas

提问by Gill Bates

Is there a way to set an option for auto-incrementing the index of pandas.DataFrame when adding new rows, or to define a function for managing creation of new indices?

有没有办法在添加新行时设置自动增加 pandas.DataFrame 索引的选项,或者定义一个用于管理新索引创建的函数?

回答by Andy Hayden

You can set ignore_index=Truewhen append-ing:

您可以ignore_index=Trueappend-ing时设置:

In [1]: df = pd.DataFrame([[1,2],[3,4]])

In [2]: row = pd.Series([5,6])

In [3]: df.append(row, ignore_index=True)
Out[3]: 
   0  1
0  1  2
1  3  4
2  5  6