A Pandas DataFrame is a two-dimensional data structure, akin to a 2D array or a table consisting of rows and columns.
You can create a simple Pandas DataFrame like this:
import pandas as pd data = { “calories”: [420, 380, 390], “duration”: [50, 40, 45] } #load data into a DataFrame object: df = pd.DataFrame(data) print(df) |
calories duration 0 420 50 1 380 40 2 390 45 |
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.
Retrieve row 0:
#refer to the row index: print(df.loc[0]) |
calories 420 duration 50 Name: 0, dtype: int64 |
Note: This example returns a Pandas Series. |
Retrieve rows 0 and 1:
#use a list of indexes: print(df.loc[[0, 1]]) |
calories duration 0 420 50 1 380 40 |
Note: When using [], the result is a Pandas DataFrame. |