I am trying to understand the meaning of the output of the following code:
import pandas as pdindex = ['index1','index2','index3']
columns = ['col1','col2','col3']
df = pd.DataFrame([[1,2,3],[1,2,3],[1,2,3]], index=index, columns=columns)
print df.index
I would expect just a list containing the index of the dataframe:
['index1, 'index2', 'index3']
however the output is:
Index([u'index1', u'index2', u'index3'], dtype='object')
This is the pretty output of the pandas.Index
object, if you look at the type it shows the class type:
In [45]:
index = ['index1','index2','index3']
columns = ['col1','col2','col3']
df = pd.DataFrame([[1,2,3],[1,2,3],[1,2,3]], index=index, columns=columns)
df.indexOut[45]:
Index(['index1', 'index2', 'index3'], dtype='object')In [46]:
type(df.index)Out[46]:
pandas.indexes.base.Index
So what it shows is that you have an Index
type with the elements 'index1' and so on, the dtype is object
which is str
if you didn't pass your list of strings for the index you get the default int index which is the new type RangeIndex
:
In [47]:
df = pd.DataFrame([[1,2,3],[1,2,3],[1,2,3]], columns=columns)
df.indexOut[47]:
RangeIndex(start=0, stop=3, step=1)
If you wanted a list of the values:
In [51]:
list(df.index)Out[51]:
['index1', 'index2', 'index3']