Pandas DataFrames
A DataFrame is pandas' two-dimensional labeled data structure, similar to a table in a database or a sheet in a spreadsheet.
What Is a DataFrame?
A DataFrame organizes data into rows and columns, much like a spreadsheet or SQL table. Each column can hold a different data type, rows are identified by an index, and columns are identified by labels. Under the hood, every column of a DataFrame is a pandas Series.
Creating a DataFrame
The most common way to build a DataFrame is from a dictionary of equal-length lists, where each key becomes a column name and each list becomes that column's values.
- From a dictionary of lists - each key becomes a column
- From a list of dictionaries - each dictionary becomes a row
- From an existing Series or group of Series
- From a 2-D NumPy array with explicit column names
- From external files such as CSV or JSON, covered in later lessons
Create a DataFrame From a Dictionary
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df)Locating Rows With loc
Use loc to Locate Rows
import pandas as pd
data = {"calories": [420, 380, 390], "duration": [50, 40, 45]}
df = pd.DataFrame(data)
print(df.loc[0])
print(df.loc[[0, 1]])By default, rows are labeled with an automatic integer index starting at 0. You can replace that with meaningful labels of your own, and then use those same labels with loc to select rows.
Named Indexes
import pandas as pd
data = {"calories": [420, 380, 390], "duration": [50, 40, 45]}
df = pd.DataFrame(data, index=["day1", "day2", "day3"])
print(df)
print(df.loc["day2"])Exercise: Pandas DataFrames
What best describes a pandas DataFrame?