Home > OS >  Difference between an array (numpy) and an index (pandas) object?
Difference between an array (numpy) and an index (pandas) object?

Time:06-01

I'm confused as to the difference between an index object such as what you would get from df.index and an array object such as df.index.values(where df is a pandas dataframe)

In which cases should I use one over the other?

Thank you

CodePudding user response:

df.index is immutable so you cannot change it. for example, if you create an index object and then try to modify an element:

test_index = pd.Index([1, 2, 3])
test_index[0] = 2 

This will throw a TypeError: Index does not support mutable operations

Whether you should use the index object or the array will depend on your use case so your question is a bit open ended. If you want to extract the index values and then modify them for some reason, then you'll want to use df.index.values, but if you are checking values within the index, then either would work.

  • Related