Pandas Series and DataFrames
pandas is the main Python library for working with tables of data. Its two building blocks are the Series and the DataFrame.
Importing pandas
By tradition, pandas is imported with the short name pd:
Example
import pandas as pdSeries: One Column
A Series is a list of values with labels, called the index:
Example
prices = pd.Series([160, 340, 520], index=["Sugar", "Rice", "Tea"])
print(prices)
print(prices["Rice"])Sugar 160 Rice 340 Tea 520 dtype: int64 340
dtype: int64 means the values are whole numbers. You can do maths on the whole Series at once, with no loop:
Example
print(prices * 1.1)Sugar 176.0 Rice 374.0 Tea 572.0 dtype: float64
DataFrame: A Table
A DataFrame is a table with rows and columns, like an Excel sheet. Each column is a Series. The easiest way to create one is from a dictionary:
Example
data = {
"name": ["Ali", "Ayesha", "Bilal", "Sana"],
"city": ["Karachi", "Lahore", "Karachi", "Quetta"],
"age": [21, 23, 19, 22],
}
df = pd.DataFrame(data)
print(df)
print(df.shape)
print(df.columns.tolist())name city age 0 Ali Karachi 21 1 Ayesha Lahore 23 2 Bilal Karachi 19 3 Sana Quetta 22 (4, 3) ['name', 'city', 'age']
shape gives (rows, columns). By default the index is 0, 1, 2, 3.
Selecting Columns
Use square brackets with a column name. One name gives a Series; a list of names gives a DataFrame:
Example
print(df["age"])
print(df[["name", "age"]])0 21
1 23
2 19
3 22
Name: age, dtype: int64
name age
0 Ali 21
1 Ayesha 23
2 Bilal 19
3 Sana 22Note the double brackets in df[["name", "age"]]. The outer brackets select, the inner brackets make a list of columns.
Selecting Rows: loc and iloc
| Method | Selects by | Example |
|---|---|---|
loc | Labels (index names and column names) | df.loc[1, "city"] |
iloc | Positions (numbers starting at 0) | df.iloc[1, 1] |
Example
print(df.loc[1])
print(df.loc[0, "city"])
print(df.iloc[2, 0])name Ayesha city Lahore age 23 Name: 1, dtype: object Karachi Bilal
You can also take a range of rows: df.loc[0:2] gives rows 0, 1 and 2 (with loc, the end is included), while df.iloc[0:2] gives rows 0 and 1 (with iloc, the end is excluded, like Python lists).
Adding Columns
Assign a list, or a calculation using other columns:
Example
df["passed"] = [True, True, False, True]
df["age_next_year"] = df["age"] + 1
print(df)name city age passed age_next_year 0 Ali Karachi 21 True 22 1 Ayesha Lahore 23 True 24 2 Bilal Karachi 19 False 20 3 Sana Quetta 22 True 23
Removing Columns
drop removes columns. It returns a new DataFrame, so save the result:
Example
df = df.drop(columns=["age_next_year"])
print(df.columns.tolist())['name', 'city', 'age', 'passed']
Quick Summary
| Task | Code |
|---|---|
| Create a DataFrame | pd.DataFrame(dictionary) |
| Size | df.shape |
| Column names | df.columns |
| One column | df["age"] |
| Several columns | df[["name", "age"]] |
| Row by label | df.loc[1] |
| Row by position | df.iloc[1] |
| New column | df["new"] = ... |
Exercise
Exercise
In a DataFrame df, which code selects the value in the first row (position 0) and the second column (position 1)?
