Skip to main content

df.index

df.index

Returns the indices (row labels) of the DataFrame.

Note:
  • The index of a DataFrame is a Series of labels that identify each row. The labels can be integers, strings, or any other hashable type.
  • Positions start at 0. Negative positions start from the end of the DataFrame.
  • The return type is bpd.Index. Use np.array() to convert it to a numpy array.

pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
2cat_002catblack159True****All you need is love and a cat.****
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!
pets.index[0]

0

pets.index[-3]

5

pets_idx = pets.set_index('ID').index
pets_idx

Index(['dog_001', 'cat_001', 'cat_002', 'dog_002', 'dog_003', 'ham_001', 'ham_002', 'cat_003'], dtype='object', name='ID')


Convert index to a numpy array. Learn more about this in the Data Format Conversion section.

idx_arr = np.array(pets_idx)
idx_arr

array(['dog_001', 'cat_001', 'cat_002', 'dog_002', 'dog_003', 'ham_001', 'ham_002', 'cat_003'], dtype=object)