Curriculum
Course: Pandas
Login
Text lesson

Named Indexes

You can assign custom indexes using the index argument.

Example

You can add a list of names to assign a unique label to each row.

import pandas as pd

data = {
  “calories”: [420380390],
  “duration”[504045]
}

df = pd.DataFrame(data, index = [“day1”“day2”“day3”])

print(df) 

Result

calories duration

day1 420 50

day2 380 40

day3 390 45

Locate Named Indexes

Use the named index with the loc attribute to retrieve the specified row(s).

Example

Retrieve the row labeled as “day2”:

#refer to the named index:
print(df.loc[“day2”])

Result

calories 380

duration 40

Name: day2, dtype: int64

Load Files Into a DataFrame

If your datasets are saved in a file, Pandas can load them into a DataFrame.

Example

Load a CSV (comma-separated) file into a DataFrame:

import pandas as pd

df = pd.read_csv(‘data.csv’)

print(df)