pythonindexingappendrowpandas

Autoincrementing option for Pandas DataFrame index


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?


Solution

  • You can set ignore_index=True when append-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