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

前端 未结 3 841
一整个雨季
一整个雨季 2020-12-13 23:57

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

相关标签:
3条回答
  • 2020-12-14 00:19

    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

    0 讨论(0)
  • 2020-12-14 00:23

    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
    
    0 讨论(0)
  • 2020-12-14 00:29

    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

    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

    0 讨论(0)
提交回复
热议问题