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
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
7 | cat_003 | cat | black | 10 | 0 | True | No, 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)