Skip to main content

A lightweight library for anonymizing and transforming data in pandas DataFrames, including masking, suppression, perturbation, permutation, generalization, and pseudonymization.

Project description

df-anonymizer

A lightweight Python library designed to apply privacy-preserving transformations on datasets in pandas.DataFrame format.
It is ideal for preparing data for research, analysis, reporting or machine learning while protecting sensitive personal information.

✨ Key Features

  • Masking: Mask email addresses and identification numbers
  • Pseudonymization: Generate unique pseudonyms with key mapping table
  • Data perturbation: Add privacy noise to age, weight, height, etc.
  • Data generalization: Bucket or reduce granularity for numeric and date values
  • Suppression: Remove sensitive columns or filter out specific records
  • Shuffling: Randomly reorder rows
  • Evaluation: Compute the k-anonymity score for your dataset

All functions are optimized to work with pandas.DataFrame structures.

📦 Installation

pip install df-anonymizer

👉 Example

import pandas as pd
from df_anonymizer import (
    pseudonymization, maskID, maskEmail, agePerturbation, weightPerturbation,
    heightPerturbation, dataPerturbation, datePerturbation, dateGeneralization,
    meanGeneralization, dataBucketing, attributeSuppression, recordSuppression,
    dataShuffling, calculateKAnonymity
)

# Pseudonymization
df = pd.DataFrame({'NRIC': ['S1234567A', 'S2345678B', 'S3456789C']})
anon_df = pseudonymization(df, 'NRIC')
print(anon_df)

# Masking
df_mask = pd.DataFrame({
    'ID': ['123456789', '987654321'],
    'Email': ['alice@example.com', 'bob@example.com']
})
df_mask = maskID(df_mask, 'ID')
df_mask = maskEmail(df_mask, 'Email')
print(df_mask)

# Perturbation
df_perturb = pd.DataFrame({
    'Age': [25, 34, 57],
    'Weight': [58.4, 72.1, 80.5],
    'Height': [163.2, 177.5, 170.0],
    'Score': [81, 92, 87],
    'Date': pd.to_datetime(['2023-01-01', '2023-01-15', '2023-02-01'])
})
df_perturb = agePerturbation(df_perturb, 'Age')
df_perturb = weightPerturbation(df_perturb, 'Weight')
df_perturb = heightPerturbation(df_perturb, 'Height')
df_perturb = dataPerturbation(df_perturb, 'Score', base_number=10)
df_perturb = datePerturbation(df_perturb, 'Date', max_days=7)
print(df_perturb)

# Generalization
df_gen = pd.DataFrame({
    'DOB': pd.to_datetime(['1990-01-01', '1995-05-15']),
    'Income': [2000, 4500, 7800],
    'Age': [22, 35, 47]
})
df_gen = dateGeneralization(df_gen, 'DOB', verbose=False)
df_gen = meanGeneralization(df_gen, 'Income', bins=3)
df_gen = dataBucketing(df_gen, 'Age', bins=[0, 30, 60], labels=['Young', 'Adult'])
print(df_gen)

# Suppression
df_sup = pd.DataFrame({
    'Name': ['Alice', 'Bob'],
    'Age': [25, 40],
    'City': ['SG', 'NY']
})
df_sup = attributeSuppression(df_sup, ['Name'])
df_sup = recordSuppression(df_sup, ['City'], [['NY']])
print(df_sup)

# Shuffling
df_shuffle = pd.DataFrame({'Name': ['A', 'B', 'C'], 'Age': [20, 30, 40]})
df_shuffle = dataShuffling(df_shuffle)
print(df_shuffle)

# k-Anonymity Evaluation
df_kanon = pd.DataFrame({
    'Age': [25, 25, 30, 30],
    'Zip': ['12345', '12345', '67890', '67890']
})
k_score = calculateKAnonymity(df_kanon, ['Age', 'Zip'])
print(f"k-anonymity score: {k_score}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

df_anonymizer-0.1.2.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

df_anonymizer-0.1.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file df_anonymizer-0.1.2.tar.gz.

File metadata

  • Download URL: df_anonymizer-0.1.2.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for df_anonymizer-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b813231049d77c8fc9a8483048967f7c0078907e41dae6c90261a27d495de403
MD5 a4130c7ea854b60883d686edbb6931e0
BLAKE2b-256 07793fec5b1e9b88614029bf0fb17dab44803cf05b03a6d42330b994296cd17a

See more details on using hashes here.

File details

Details for the file df_anonymizer-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: df_anonymizer-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for df_anonymizer-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8bb53d1a3b68b36cde30aeb787530e7b7dfa349b44080ce25855c9cdd13211be
MD5 8735cbee09ef57bde311391ac8380416
BLAKE2b-256 d20cc1236a5ea95afcf0a01c059365567c48e8bde00b233303e6dc87df60c90d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page