When following the prescribed way for setting index in pandas with set_index function, the index does not change in the output and loc function doesn't select the labels consequently. I am using Spyder for running the program.
import pandas as pd
df = pd.DataFrame({'age':[30, 2, 12, 4, 32, 33, 69],
'color':['blue', 'green', 'red', 'white', 'gray', 'black',
'red'],
'food':['Steak', 'Lamb', 'Mango', 'Apple', 'Cheese',
'Melon', 'Beans'],
'height':[165, 70, 120, 80, 180, 172, 150],
'score':[4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2],
'state':['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'],
'Name':['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean',
'Christina', 'Cornelia']})
df.set_index('color')
print(df.head(5))
The above is returning the following output.
age color food height score state Name
0 30 blue Steak 165 4.6 NY Jane``
1 2 green Lamb 70 8.3 TX Nick
2 12 red Mango 120 9.0 FL Aaron
3 4 white Apple 80 3.3 AL Penelope
4 32 gray Cheese 180 1.8 AK Dean