DataFrames
Creating a DataFrame
The most common ways are from a dictionary of columns, or from a list of row dictionaries:
Python
import pandas as pd
by_columns = pd.DataFrame({
"name": ["Asha", "Leo", "Mei"],
"age": [21, 30, 24],
})
by_rows = pd.DataFrame([
{"name": "Asha", "age": 21},
{"name": "Leo", "age": 30},
{"name": "Mei", "age": 24},
])
print(by_columns.equals(by_rows))
print(by_rows)Reading CSV data
Real data usually arrives as a CSV file: pd.read_csv("sales.csv"). Here we read CSV text directly:
Python
import pandas as pd
from io import StringIO
csv = """order_id,customer,product,quantity,price
101,Asha,Keyboard,1,89.99
102,Leo,Monitor,2,279.00
103,Asha,SSD,1,109.00
104,Mei,Headphones,1,199.00
105,Raj,Mouse,4,24.50
"""
orders = pd.read_csv(StringIO(csv))
print(orders)First look at any dataset
Four commands you'll run on every new dataset:
Python
import pandas as pd
from io import StringIO
csv = """order_id,customer,product,quantity,price
101,Asha,Keyboard,1,89.99
102,Leo,Monitor,2,279.00
103,Asha,SSD,1,109.00
104,Mei,Headphones,1,199.00
105,Raj,Mouse,4,24.50
"""
orders = pd.read_csv(StringIO(csv))
print(orders.shape) # (rows, columns)
print(orders.head(2)) # first rows
orders.info() # column names, types, missing values
print(orders.describe()) # statistics for numeric columnsSelecting columns
Python
import pandas as pd
df = pd.DataFrame({"name": ["Asha", "Leo"], "age": [21, 30], "city": ["Mumbai", "London"]})
print(df["name"]) # one column: a Series
print(df[["name", "city"]]) # several columns: a DataFrame (note the double brackets)
print(df.columns.tolist())Adding a column
Python
import pandas as pd
orders = pd.DataFrame({"product": ["Keyboard", "Monitor", "Mouse"], "quantity": [1, 2, 4], "price": [89.99, 279.0, 24.5]})
orders["total"] = orders["quantity"] * orders["price"]
orders["big_order"] = orders["total"] > 200
print(orders)Column arithmetic is vectorised, just like NumPy: one line computes every row.
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.