0

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
Itchydon
  • 2,572
  • 6
  • 19
  • 33

1 Answers1

1

As @ALollz said in their response, your problem is the useage of the .set_index method. Specifically, used the way you have it returns a dataframe with adjusted index, while you're expecting it to work directly on the one you used the method on. To achieve that, you need to use the inplace=True parameter.

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 = df.set_index('color')
# or df.set_index('color', inplace=True)
print(df.head(5))
molybdenum42
  • 343
  • 1
  • 9