How To Encrypt And Decrypt Pandas Dataframe With Decryption Key?
Solution 1:
I'd recommend the python itsdangerous
library. Here is a quick example:
from itsdangerous import URLSafeSerializer
s = URLSafeSerializer('secret-key')
print(s.dumps([1, 2, 3, 4]))
# 'WzEsMiwzLDRd.wSPHqC0gR7VUqivlSukJ0IeTDgo'print(s.loads('WzEsMiwzLDRd.wSPHqC0gR7VUqivlSukJ0IeTDgo'))
# [1, 2, 3, 4]
The secret-key
can be shared between you and the other trusted party to decrypt the strings or columns.
This does rely on serialization however and some python data types aren't easily serialized, but if you just need a column name or something like that, this could work well.
I would like to add a qualification here that this process only obfuscates the data, but does not actually encrypt it. I did not fully understand that when I originally answered this question. This obfuscation may be enough for your needs, but please be aware! From the docs:
The receiver can decode the contents and look into the package, but they can not modify the contents unless they also have your secret key. Docs
Solution 2:
I think you can do this way
key=dict(zip(np.arange(len(df)),df.id))
df.id=np.arange(len(df))
**# for the person do not have the key**
df
Out[640]:
id001122
**# for the person who havde the key**
df.id=df.id.map(key.get)
df
Out[642]:
id011223
Solution 3:
You could use AES from the Crypto.Cipher library. I wrote some helper functions to encrypt sets of columns in a pandas dataframe. Examples here if helpful: https://github.com/bennywij/junk-drawer/blob/master/secret_pandas.py
Solution 4:
You can use cryptpandas
.
As an example, if you have a pandas dataframe
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3],
'B': ['one', 'one', 'four']})
you can encrypt it as
import cryptpandas as crp
crp.to_encrypted(df, password='mypassword123', path='file.crypt')
and decrypt it as
decrypted_df = crp.read_encrypted(path='file.crypt', password='mypassword123')
P.S. More info here.
Post a Comment for "How To Encrypt And Decrypt Pandas Dataframe With Decryption Key?"