How to Get the Index of a Pandas DataFrame ?

Spread the love

To get the index of a Pandas DataFrame we can use the DataFrame index method (pandas.DataFrame.index ). The method returns the index of the DataFrame.

Let’s read a dataset to illustrate it.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv'
df = pd.read_csv(url)
df.head()

Now, to get the index of the dataframe we can use

df.index
#output
RangeIndex(start=0, stop=100, step=1)

To get the index as a list we can use

df.index.to_list()

This will return the index in a list.

We can also use a for loop to iterate over the index.

for i in df.index:
    print(i)

Rating: 1 out of 5.

Leave a Reply