Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 822
Gill Bates Avatar asked Feb 08 '13 17:02

Gill Bates


People also ask

How do I select a specific index in pandas?

If you'd like to select rows based on integer indexing, you can use the . iloc function. If you'd like to select rows based on label indexing, you can use the . loc function.

How do I change the index of a DataFrame in pandas?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

Can you index a pandas DataFrame?

To set an index for a Pandas DataFrame, you can use the Pands . set_index method.

Does ILOC include index?

iloc is an integer-based method. This means that iloc will consider the names or labels of the index when we are slicing the dataframe.


1 Answers

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
like image 83
Andy Hayden Avatar answered Sep 23 '22 12:09

Andy Hayden