Curriculum
Course: Pandas
Login
Text lesson

Pandas DataFrames

What is a DataFrame?

A Pandas DataFrame is a two-dimensional data structure, akin to a 2D array or a table consisting of rows and columns.

Example

You can create a simple Pandas DataFrame like this:

import pandas as pd

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

#load data into a DataFrame object:
df = pd.DataFrame(data)

print(df) 

Result

calories duration

0 420 50

1 380 40

2 390 45

Locate Row

As shown in the result above, the DataFrame resembles a table with rows and columns.

Pandas uses the loc attribute to retrieve one or more specified rows.

Example

Retrieve row 0:

#refer to the row index:
print(df.loc[0])

Result

calories 420

duration 50

Name: 0, dtype: int64

Note: This example returns a Pandas Series.

Example

Retrieve rows 0 and 1:

#use a list of indexes:
print(df.loc[[01]])

Result

calories duration

0 420 50

1 380 40

Note: When using [], the result is a Pandas DataFrame.