You can assign custom indexes using the index
argument.
You can add a list of names to assign a unique label to each row.
import pandas as pd data = { “calories”: [420, 380, 390], “duration”: [50, 40, 45] } df = pd.DataFrame(data, index = [“day1”, “day2”, “day3”]) print(df) |
calories duration day1 420 50 day2 380 40 day3 390 45 |
Use the named index with the loc
attribute to retrieve the specified row(s).
Retrieve the row labeled as “day2”:
#refer to the named index: print(df.loc[“day2”]) |
calories 380 duration 40 Name: day2, dtype: int64 |
If your datasets are saved in a file, Pandas can load them into a DataFrame.
Load a CSV (comma-separated) file into a DataFrame:
import pandas as pd df = pd.read_csv(‘data.csv’) print(df) |