Data Privacy and Anonymization in Python
Rebeca Gonzalez
Data engineer
非敏感的可辨識個人資訊也可能是準識別子。

與其他準識別子結合時,可能足以識別個人。


針對準識別子的匿名化技巧,可降低資料外洩風險。

原始 DataFrame
name phone
0 Cassandra Nelson 4399406975395
1 Brian Moss 0389407128613
2 Melody Gill 8283308773967
3 Sandra Huber 4366608954250
4 Patricia Webster 4466462475574
名稱已遮罩的 DataFrame
name phone
0 xxxx 4399406975395
1 xxxx 0389407128613
2 xxxx 8283308773967
3 xxxx 4366608954250
4 xxxx 4466462475574
以其他字元(如「x」)取代敏感值,稱為資料遮罩。
# Explore the DataFrame
df.head()
country card_number email
0 Finland 3546746666030419 [email protected]
1 Belarus 4303032415762821 [email protected]
2 Turkmenistan 4536883671157 [email protected]
3 Puerto Rico 3568819286614160 [email protected]
4 Angola 2514167462583016 [email protected]
# Uniformly mask the card number colum df['card_number'] = '****'# See resulting DataFrame df.head()
country card_number email
0 Finland **** [email protected]
1 Belarus **** [email protected]
2 Turkmenistan **** [email protected]
3 Puerto Rico **** [email protected]
4 Angola **** [email protected]

# Mask username from email df2['email'] = df2['email'].apply(lambda s: s[0] + '****' + s[s.find('@'):] )# See the resulting pseudonymized data df2.head()
country card_number email
0 Finland 3546746666030419 f****@gmail.com
1 Belarus 4303032415762821 m****@gmail.com
2 Turkmenistan 4536883671157 a****@gmail.com
3 Puerto Rico 3568819286614160 k****@gmail.com
4 Angola 2514167462583016 d****@gmail.com

# Import Faker class from faker import Faker# Create fake data generator fake_data = Faker()# Generate a credit card number fake_data.credit_card_number()
3542216874440804
# Mask card number with new generated data using a lambda function df['card_number'] = df['card_number'].apply(lambda x: fake_data.credit_card_number())# See the resulting pseudonymized data df.head()
country card_number email
0 Finland 3596625386355448 [email protected]
1 Belarus 376297265347524 [email protected]
2 Turkmenistan 4377494880888682 [email protected]
3 Puerto Rico 30553931809810 [email protected]
4 Angola 4241735748382 [email protected]
fake_data.name()
'Kelly Clark'
fake_data.name_male()
'Antonio Henderson'
fake_data.name_female()
'Jennifer Ortega'
Data Privacy and Anonymization in Python