在 Python pandas 中,从 1 而不是 0 开始行索引而不创建额外的列

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

In Python pandas, start row index from 1 instead of zero without creating additional column

pythonpandasindexingdataframe

提问by Bram Vanroy

I know that I can reset the indices like so

我知道我可以像这样重置索引

df.reset_index(inplace=True)

but this will start the index from 0. I want to start it from 1. How do I do that without creating any extra columns and by keeping the index/reset_index functionality and options? I do notwant to create a new dataframe, so inplace=Trueshould still apply.

但这将从0. 我想从1. 如何在不创建任何额外列并保留 index/reset_index 功能和选项的情况下做到这一点?我希望创建一个新的数据帧,所以inplace=True应该仍然适用。

采纳答案by EdChum

Just assign directly a new index array:

只需直接分配一个新的索引数组:

df.index = np.arange(1, len(df) + 1)

Example:

例子:

In [151]:

df = pd.DataFrame({'a':np.random.randn(5)})
df
Out[151]:
          a
0  0.443638
1  0.037882
2 -0.210275
3 -0.344092
4  0.997045
In [152]:

df.index = np.arange(1,len(df)+1)
df
Out[152]:
          a
1  0.443638
2  0.037882
3 -0.210275
4 -0.344092
5  0.997045

Or just:

要不就:

df.index = df.index + 1

If the index is already 0 based

如果索引已经基于 0

TIMINGS

时机

For some reason I can't take timings on reset_indexbut the following are timings on a 100,000 row df:

出于某种原因,我无法计时,reset_index但以下是 100,000 行 df 的计时:

In [160]:

%timeit df.index = df.index + 1
The slowest run took 6.45 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 107 μs per loop


In [161]:

%timeit df.index = np.arange(1, len(df) + 1)
10000 loops, best of 3: 154 μs per loop

So without the timing for reset_indexI can't say definitively, however it looks like just adding 1 to each index value will be faster if the index is already 0based

所以没有时间reset_index我不能明确地说,但是如果索引已经0基于,看起来只是向每个索引值添加 1 会更快

回答by hakuna_code

You can also specify the start value using index range like below. RangeIndex is supported in pandas.

您还可以使用如下所示的索引范围指定起始值。熊猫支持 RangeIndex。

#df.index

default value is printed, (start=0,stop=lastelement, step=1)

打印默认值,(start=0,stop=lastelement, step=1)

You can specify any start value range like this:

您可以指定任何起始值范围,如下所示:

df.index = pd.RangeIndex(start=1, stop=600, step=1)

Refer: pandas.RangeIndex

参考:pandas.RangeIndex