77

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 not want to create a new dataframe, so inplace=True should still apply.

0

6 Answers 6

128

Just assign directly a new index array:

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

Or if the index is already 0 based, just:

df.index += 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

TIMINGS

For some reason I can't take timings on reset_index but the following are timings on a 100,000 row 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_index I can't say definitively, however it looks like just adding 1 to each index value will be faster if the index is already 0 based

Sign up to request clarification or add additional context in comments.

1 Comment

np.arange(len(df)) + 1 is equivalent, right? That's more readable to me.
12

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

#df.index

default value is printed, (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

Comments

4

For this, you can do the following(I created an example dataframe):

price_of_items = pd.DataFrame({
"Wired Keyboard":["$7","4.3","12000"],"Wireless Keyboard":["$13","4.6","14000"]
                             })
price_of_items.index += 1

Comments

4

One possibility is to simply increment each index value by 1 (which changes the index in-place).

df = pd.DataFrame({'col': [1, 2, 3]})
df.index += 1

Another is to assign a new range index, that starts from 1, using set_axis().

df = pd.DataFrame({'col': [1, 2, 3]})
df = df.set_axis(range(1, len(df)+1))

In fact, since set_axis() assigns a new object to the index, i.e. resets the index, it can be used instead of reset_index().

It is especially useful if you need to make the index to start from 1 in a pipeline (where assigning or incrementing index wouldn't work).

df = pd.DataFrame({'col': [4, 1, 2, 3]})
df = (
    df
    .reset_index()
    .set_axis(range(1, len(df)+1))
)

result1

or the dataframe shape needs to be modified in the pipeline (e.g. using query()), pipe() could be used.

df = pd.DataFrame({'col': [4, 1, 2, 3]})
df = (
    df
    .query('col > 2')
    .pipe(lambda x: x.set_axis(range(1, len(x)+1)))
)

result2

Comments

0

Using rename avoids assignment, which makes it convenient in a method chain:

(
    pd.DataFrame({'col': ['a', 'b', 'c']})
    .rename(lambda x: x + 1)  # Rename each index value with the old value + 1
)
  col
1   a
2   b
3   c

Comments

0

This may be the simplest answer and will do the trick without forcing you to alter your data frame:

df.set_axis(range(1, len(df) + 1))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.