Lesson 4 of 13 · 2 min
DataFrame
Analogy: A DataFrame is a table — rows are observations (people, transactions), columns are features (age, salary, city).
import numpy as np
import pandas as pd
# Create DataFrame from numpy array
df = pd.DataFrame(
np.arange(0, 20).reshape(5, 4),
index=['Row1', 'Row2', 'Row3', 'Row4', 'Row5'],
columns=['Column1', 'Column2', 'Column3', 'Column4']
)
print(df)
# Output:
# Column1 Column2 Column3 Column4
# Row1 0 1 2 3
# Row2 4 5 6 7
# Row3 8 9 10 11
# Row4 12 13 14 15
# Row5 16 17 18 19
import pandas as pd
# Create DataFrame from dictionary
data = {"calories": [420, 380, 390], "duration": [50, 40, 45]}
df = pd.DataFrame(data)
print(df)
# Output:
# calories duration
# 0 420 50
# 1 380 40
# 2 390 45
