Last checked
Kenya's Data Protection Act 2019 treats pseudonymised data as still personal, because someone holding the key can link it back. Anonymised data, where people can no longer be identified, is outside the Act's focus. In pandas, hash phone numbers with a secret salt, generalise dates and amounts, then check k-anonymity.
McTaba runs coding and data bootcamps in Kenya. This guide is written to be useful whether or not you ever join one.
Picture a client sending you an export of 50,000 M-Pesa payments for analysis. Each row has a phone number, a name, an amount and a timestamp. You want to build a model. Every one of those rows is a real person's money trail.
This guide covers two things. First, what the law says about handling that file. Second, how to strip it down in pandas so your analysis can go ahead with less risk. It is general information and not legal advice. Ask a lawyer or the regulator about your own case.
It does not repeat the full compliance walk-through in our Data Protection Act guide for AI builders. Read that for registration, rights and fines. For where this skill fits in a career, see the data science career guide for Kenya.
The Data Protection Act, 2019 is on Kenya Law. The Office of the Data Protection Commissioner (ODPC) publishes guidance and the Act's text at odpc.go.ke. Section numbers below were checked on 3 November 2026 against a published copy of the Act, so confirm them on Kenya Law before you quote them.
Two lessons for a data scientist. Hashing a phone number is pseudonymisation, not anonymisation, so the Act still applies. And minimising early, by dropping columns you do not need, is the cheapest compliance step there is.
If you want to learn to work with such data responsibly, our Data Science programme in Nairobi builds its projects on M-Pesa style data.
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
For example, imagine a made-up file called payments.csv with the columns phone, name, amount, timestamp and till_area. None of it is real. The steps are in order of how much each reduces risk.
import pandas as pd
df = pd.read_csv("payments.csv")
df = df.drop(columns=["name"]) # you never model on names
A plain hash of a Kenyan phone number is easy to reverse. The list of possible numbers is short enough that an attacker can hash every one of them. A secret salt (a random string you keep outside the dataset) blocks that.
import hmac, hashlib, os
SALT = os.environ["PHONE_SALT"].encode() # keep out of the repo
def pseudonym(phone):
return hmac.new(SALT, str(phone).encode(), hashlib.sha256).hexdigest()[:16]
df["customer_id"] = df["phone"].map(pseudonym)
df = df.drop(columns=["phone"])
The same phone always maps to the same customer_id, so you can still count repeat customers. Store the salt separately and restrict who can read it. This is the "additional information, kept separately" the Act's definition talks about.
Exact timestamps and exact amounts make people findable. Coarsen them.
df["timestamp"] = pd.to_datetime(df["timestamp"])
df["hour_band"] = df["timestamp"].dt.floor("3h")
df["day"] = df["timestamp"].dt.date
df["amount_band"] = pd.cut(
df["amount"],
bins=[0, 100, 500, 1000, 5000, 20000, 150000],
)
df = df.drop(columns=["timestamp", "amount"])
You lose precision, so ask what the analysis needs. A fraud model may need exact amounts. A footfall study does not. Decide per purpose, not once for all.
k-anonymity is a simple test. Choose the columns someone could combine to pick out a person, called quasi-identifiers. Here: till_area, day and amount_band. Every combination should appear at least k times, often 5. A combination that appears once is a person who can be found.
quasi = ["till_area", "day", "amount_band"]
sizes = df.groupby(quasi, observed=True).size()
print("smallest group:", sizes.min())
risky = sizes[sizes < 5]
print(len(risky), "combinations appear fewer than 5 times")
If risky combinations exist, generalise more (weekly instead of daily, wider amount bands, bigger areas) or drop those rows. Run the check again until the smallest group reaches your k.
Know the limits. k-anonymity does not stop someone who already knows facts about a target, and a very rare event, such as one large payment at a small stage, can still stand out. For data you will publish, take advice. For data you only analyse inside your team, pseudonymisation plus minimisation plus access control is a sensible baseline.
The same habits belong in the team data quality checklist, where "what is personal" is one of four governance lines. For the full analysis workflow on a statement, see analysing an M-Pesa statement in Python.
Our Data Science programme in Nairobi teaches these habits next to the models, because a model built on data you were not allowed to hold helps nobody.
KES 140,000
Data Science to AI Engineering: 5 months full time or 8 months part time, hybrid in Nairobi. Fraud, credit and M-Pesa data from Week 1, and every model deployed. Starts 11 January 2027.
See the Data Science programme