0

I have a dictionary using that i am forming a dataframe. I have list which i want to set it as an index.

import numpy as np
import pandas as pd
data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
        'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
        'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df=pd.DataFrame(data)
df.set_index(labels)

I am receiving KeyError: 'a' as an error.

3
  • Use df.index = labels? Commented Jul 21, 2018 at 13:20
  • or df.set_index([labels]) or df.set_axis(labels,inplace=True) Commented Jul 21, 2018 at 13:21
  • got it bro..Thanks Commented Jul 21, 2018 at 13:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.