If you copy and paste the same block of cleaning code for every new file, it is time to write a function. Functions make your analysis shorter, easier to test and much easier to repeat next month.
The basic shape
def gst_amount(price, rate=0.18):
"""Return the GST on a price. rate is a fraction, 0.18 for 18%."""
return round(price * rate, 2)
gst_amount(500) # uses the default rate
gst_amount(500, 0.05) # a different ratedefstarts a function, followed by its name and parameters.- Default values make parameters optional.
returnsends a value back. Without it, the function returnsNone.- The docstring explains what the function does; your future self will thank you.
Keyword arguments make calls readable
gst_amount(price=500, rate=0.05)For functions with several parameters, naming them at the call avoids mistakes about their order.
Return several values
def min_max_avg(values):
return min(values), max(values), sum(values) / len(values)
low, high, avg = min_max_avg([420, 380, 510, 465])Python returns a tuple, which you can unpack into separate names.
A real cleaning function
def clean_city(name):
if name is None:
return "Unknown"
name = name.strip().title()
aliases = {"Bangalore": "Bengaluru", "Bombay": "Mumbai", "Gurgaon": "Gurugram"}
return aliases.get(name, name)Used with pandas: df["city"] = df["city"].map(clean_city).
Scope: variables inside stay inside
Variables created inside a function exist only while it runs. This keeps functions independent. Pass inputs as parameters and return outputs, rather than reading and changing global variables.
The mutable default trap
def add_order(order, orders=[]): # bug
orders.append(order)
return ordersThe default list is created once and shared between calls, so orders pile up across unrelated calls. The fix:
def add_order(order, orders=None):
if orders is None:
orders = []
orders.append(order)
return ordersThis is a well-known interview question.
*args and **kwargs
def total(*amounts):
return sum(amounts)
total(100, 250, 75)*args collects any number of positional arguments; **kwargs collects keyword arguments into a dictionary.
Recursion, briefly
A function can call itself. It is useful for nested structures (folders in folders, categories in categories) but rarely needed in everyday analysis. Every recursive function needs a stopping condition.
Good habits
- One function, one job.
- Clear names:
clean_city, notfunc1. - Keep functions short enough to read in one screen.
- Test them on a few small inputs, including awkward ones (empty values, unexpected text).
Practise
Our functions set builds from simple returns to defaults and recursion, and practice test 2 mixes functions with lambdas and comprehensions.
Practise what you just read
Reading helps; writing the code yourself is what sticks. Start here: