데이터 프레임의 문자열을 암호화하는 방법을 찾고있는 것 같습니다. cryptography
등의 파이썬 암호화 라이브러리가 있습니다. 사용 방법은 간단합니다. 각 요소에 적용하기 만하면됩니다.
import pandas as pd
from cryptography.fernet import Fernet
df =pd.DataFrame([{'a':'a','b':'b'}, {'a':'a','b':'c'}])
f = Fernet('password')
res = df.applymap(lambda x: f.encrypt(byte(x, 'utf-8'))
# Decrypt
res.applymap(lambda x: f.decrypt(x))
아마도 보안 측면에서 가장 좋은 방법이지만 긴 바이트/문자열을 생성하고 보면보기가 어려울 수 있습니다.
# 'a' -> b'gAAAAABaRQZYMjB7wh-_kD-VmFKn2zXajMRUWSAeridW3GJrwyebcDSpqyFGJsCEcRcf68ylQMC83G7dyqoHKUHtjskEtne8Fw=='
그래서 당신의 문제를 해결하는 또 다른 간단한 방법은 값으로 키를 매핑하고 새 키가있는 경우 새 값을 생성하는 함수를 만드는 것입니다. 당신이 ML을하고 있다면
mapper = {}
def encode(string):
if x not in mapper:
# This part can be changed with anything really
# Such as mapper[x]=randint(-10**10,10**10)
# Just ensure it would not repeat
mapper[x] = len(mapper)+1
return mapper[x]
res = df.applymap(encode)