Pandasdrill

pandas for Excel users

You do not need to learn pandas from zero if you already know Excel. Almost everything you do in a workbook has a direct pandas form. This page maps the features analysts use most, with code you can run.

Filter and sort

The examples use two small tables: orders (order_id, customer, date, amount, status) with 8 rows, one of them cancelled, and customers (customer, country, segment) with 5 rows, one of whom never ordered.

AutoFilter on two columns

Each condition is a True/False mask. Join them with & and wrap each in parentheses.

Excel: Data > Filter: status = completed, amount >= 150

pandas

orders[(orders["status"] == "completed") & (orders["amount"] >= 150)]

Result:

   order_id customer       date  amount     status
1         2    birch 2024-01-12   300.0  completed
4         5     dune 2024-02-10   210.0  completed
5         6    birch 2024-02-20   150.0  completed
7         8   cobalt 2024-03-15   500.0  completed

Sort by two columns

Give the columns and a direction for each.

Excel: Data > Sort: customer A to Z, then amount largest to smallest

pandas

orders.sort_values(["customer", "amount"], ascending=[True, False]).head(4)[["customer", "amount"]]

Result:

  customer  amount
0     acme   120.0
2     acme    80.0
6     acme    60.0
1    birch   300.0

Write it yourself: Sort by two columns, checked on two datasets.

Try it yourself

The Excel formula would be SUMIFS(amount, status, "completed", amount, ">=50"). Return the total amount of orders that are completed and worth at least 50, as a single number.

orders: 12 rows, first 4 shown

order_idcustomerdateamountstatus
1"acme"2024-01-05120"completed"
2"birch"2024-01-12300"completed"
3"acme"2024-01-2880"completed"
4"cobalt"2024-02-0345"cancelled"

Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.

SUMIFS, COUNTIFS and IFS

SUMIFS

Filter the rows, pick the column, add it up.

Excel: =SUMIFS(D:D, E:E, "completed", D:D, ">=100")

pandas

orders.loc[(orders["status"] == "completed") & (orders["amount"] >= 100), "amount"].sum()

Result:

1280.0

Write it yourself: Excel SUMIFS: a total with two conditions, checked on two datasets.

COUNTIFS for every customer at once

Where Excel needs one formula per customer, pandas filters once and counts per group.

Excel: =COUNTIFS(B:B, G2, E:E, "completed", D:D, ">=100")

pandas

big = orders[(orders["status"] == "completed") & (orders["amount"] >= 100)]
big.groupby("customer").size().reset_index(name="big_orders")

Result:

  customer  big_orders
0     acme           1
1    birch           2
2   cobalt           1
3     dune           1

Write it yourself: Excel COUNTIFS: count per customer, checked on two datasets.

IF

np.where takes a condition, the value if true and the value if false, for the whole column.

Excel: =IF(D2>=150, "large", "small")

pandas

orders.assign(size=np.where(orders["amount"] >= 150, "large", "small"))[["order_id", "amount", "size"]].head(4)

Result:

   order_id  amount   size
0         1   120.0  small
1         2   300.0  large
2         3    80.0  small
3         4    45.0  small

IFS with three outcomes

np.select checks a list of conditions in order and uses the default when none is true.

Excel: =IFS(D2<100, "small", D2<300, "medium", TRUE, "large")

pandas

size = np.select([orders["amount"] < 100, orders["amount"] < 300], ["small", "medium"], default="large")
orders.assign(size=size)[["order_id", "amount", "size"]].head(4)

Result:

   order_id  amount    size
0         1   120.0  medium
1         2   300.0   large
2         3    80.0   small
3         4    45.0   small

Write it yourself: Excel IFS: three size bands, checked on two datasets.

Lookups and pivot tables

XLOOKUP or VLOOKUP

A left merge keeps every row and brings in the matching column.

Excel: =XLOOKUP(B2, customers!A:A, customers!B:B)

pandas

orders.merge(customers[["customer", "country"]], on="customer", how="left").head(3)[["order_id", "customer", "country"]]

Result:

   order_id customer country
0         1     acme      US
1         2    birch      UK
2         3     acme      US

Write it yourself: Excel XLOOKUP: bring a price into each row, checked on two datasets.

A pivot table

index is the Rows area, columns is the Columns area, values and aggfunc are the Values area. fill_value replaces the blanks.

Excel: Insert > PivotTable: Rows customer, Columns month, Values Sum of amount

pandas

orders.assign(month=orders["date"].dt.strftime("%Y-%m")).pivot_table(index="customer", columns="month", values="amount", aggfunc="sum", fill_value=0)

Result:

month     2024-01  2024-02  2024-03
customer                           
acme        200.0      0.0     60.0
birch       300.0    150.0      0.0
cobalt        0.0     45.0    500.0
dune          0.0    210.0      0.0

Write it yourself: Pivot table with a fill value, checked on two datasets.

Cleaning

Remove Duplicates

drop_duplicates on the columns that define a duplicate.

Excel: Data > Remove Duplicates on customer

pandas

orders.drop_duplicates("customer")[["order_id", "customer"]]

Result:

   order_id customer
0         1     acme
1         2    birch
3         4   cobalt
4         5     dune

Write it yourself: Remove duplicate records, checked on two datasets.

TRIM, LOWER and text to number

The .str methods apply a text function to the whole column; astype converts it.

Excel: =LOWER(TRIM(A2)) and =VALUE(SUBSTITUTE(SUBSTITUTE(B2, "$", ""), ",", ""))

pandas

raw.assign(region=raw["region"].str.strip().str.lower(), amount=raw["amount"].str.replace("[$,]", "", regex=True).astype(float))

Result:

  region  amount
0   east  1200.5
1   east   300.0
2   west    45.0

Write it yourself: Clean messy money text, checked on two datasets.

Practice these live

Each exercise runs real pandas in your browser and checks your result on the sample and on a second dataset. Your first 8 are free, with no account.

Start practicing free

Related guides