Skip to content Skip to sidebar Skip to footer

Pandas Dataframe: Meaning Of .index

I am trying to understand the meaning of the output of the following code: import pandas as pd index = ['index1','index2','index3'] columns = ['col1','col2','col3'] df = pd.DataFr

Solution 1:

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.index

Out[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.index

Out[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']

Post a Comment for "Pandas Dataframe: Meaning Of .index"