Skip to content
Advertisement

How to encrypt and decrypt pandas dataframe with decryption key?

I have a df with 300 columns but there is one column ID that I want to encrypt and allow anyone else with a key to decrypt if I give them the df as a csv.

Is this possible?

I know how to hash a column, but as far as I have read I can not unhash it or give someone a key to unhash it.

Thank you in advance.

edit:

df

id
1
2
3

@Wen is this a good example:

(1:2), (2:3),(3:4)

new df

id
2
3
4

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement