start index at 1 for Pandas DataFrame

134,087

Solution 1

Index is an object, and default index starts from 0:

>>> result.index
Int64Index([0, 1, 2], dtype=int64)

You can shift this index by 1 with

>>> result.index += 1 
>>> result.index
Int64Index([1, 2, 3], dtype=int64)

Solution 2

Just set the index before writing to CSV.

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

And then write it normally.

Solution 3

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

Working example:

import pandas as pdas
dframe = pdas.read_csv(open(input_file))
dframe.index = dframe.index + 1

Solution 4

Another way in one line:

df.shift()[1:]

Solution 5

This worked for me

 df.index = np.arange(1, len(df)+1)
Share:
134,087
Clark Fitzgerald
Author by

Clark Fitzgerald

Updated on July 13, 2022

Comments

  • Clark Fitzgerald
    Clark Fitzgerald almost 2 years

    I need the index to start at 1 rather than 0 when writing a Pandas DataFrame to CSV.

    Here's an example:

    In [1]: import pandas as pd
    
    In [2]: result = pd.DataFrame({'Count': [83, 19, 20]})
    
    In [3]: result.to_csv('result.csv', index_label='Event_id')                               
    

    Which produces the following output:

    In [4]: !cat result.csv
    Event_id,Count
    0,83
    1,19
    2,20
    

    But my desired output is this:

    In [5]: !cat result2.csv
    Event_id,Count
    1,83
    2,19
    3,20
    

    I realize that this could be done by adding a sequence of integers shifted by 1 as a column to my data frame, but I'm new to Pandas and I'm wondering if a cleaner way exists.

  • yourstruly
    yourstruly almost 8 years
    somehow it changes index name - so proper order with naming is: df.index+=1;df.index.name='name'
  • Dung
    Dung over 7 years
    where np is import like so: import numpy as np
  • Natesh bhat
    Natesh bhat almost 6 years
    it should be df.index = arange( 1, len(df) + 1)
  • Armali
    Armali over 3 years
    This drops the last row.
  • santhosh_dj
    santhosh_dj about 3 years
    efficient way : df.index = range(1, df.shape[0] + 1)