如何编码和解码python pandas中的列?

时间:2018-09-24 04:40:51

标签: python pandas

load = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,0],
                   'D':[1,3,5,4,2,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

如何对F列进行编码和解码。

预期输出:

应该再有两列具有编码和解码值

    A   B   C   D   E   F Encoded_Column           Decoded_Column
0   a   4   7   1   5   a   <!--Encoded Value-->    a
1   b   5   8   3   3   a   <!--Encoded Value-->    a
2   c   4   9   5   6   a   <!--Encoded Value-->    a 
3   d   5   4   4   9   b   <!--Encoded Value-->    b
4   e   5   2   2   2   b   <!--Encoded Value-->    b
5   f   4   0   0   4   b   <!--Encoded Value-->    b

2 个答案:

答案 0 :(得分:1)

您可以使用this solution对熊猫实施的Series.apply

        //Observable<List<System>>
        var temp = apiService.getSystems(accessToken = ACCESS_TOKEN, id = LOCATION_ID)

        temp.flatMapIterable { //it:List<System>
            it
        }.flatMap ({ t: System ->
            apiService.getEquipments(ACCESS_TOKEN, t.id)
        }, {
            t1: System, t2: ListResponse<Equipment> ->
            mutableMapOf(Pair(t1, t2))
        })
        .toList()
        .subscribe {
            t: MutableList<MutableMap<System, 
               ListResponse<Equipment>>>? -> {

            }
        }

另一个solution

List<Pair<System, List<Equipments>>>

或使用from Crypto.Cipher import XOR import base64 def encrypt(key, plaintext): cipher = XOR.new(key) return base64.b64encode(cipher.encrypt(plaintext)) def decrypt(key, ciphertext): cipher = XOR.new(key) return cipher.decrypt(base64.b64decode(ciphertext)) load['Encoded_Column'] = load['F'].apply(lambda x: encrypt('password',x)) load['Decoded_Column'] = (load['Encoded_Column'].apply(lambda x: decrypt('password', x)) .str.decode("utf-8")) print (load) A B C D E F Encoded_Column Decoded_Column 0 a 4 7 1 5 a b'EQ==' a 1 b 5 8 3 3 a b'EQ==' a 2 c 4 9 5 6 a b'EQ==' a 3 d 5 4 4 9 b b'Eg==' b 4 e 5 2 2 2 b b'Eg==' b 5 f 4 0 0 4 b b'Eg==' b

import base64
def encode(key, clear):
    enc = []
    for i in range(len(clear)):
        key_c = key[i % len(key)]
        enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
        enc.append(enc_c)
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()

def decode(key, enc):
    dec = []
    enc = base64.urlsafe_b64decode(enc).decode()
    for i in range(len(enc)):
        key_c = key[i % len(key)]
        dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
        dec.append(dec_c)
    return "".join(dec)

load['Encoded_Column'] = load['F'].apply(lambda x: encode('password',x))
load['Decoded_Column'] = load['Encoded_Column'].apply(lambda x: decode('password', x))

答案 1 :(得分:0)

import pandas as pd
import binascii

load = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,0],
                   'D':[1,3,5,4,2,0],
                   'E':[5,3,6,9,2,4],
                   'F':[binascii.hexlify(x.encode()) for x in 'aaabbb']
                    })

   A  B  C  D  E      F
0  a  4  7  1  5  b'61'
1  b  5  8  3  3  b'61'
2  c  4  9  5  6  b'61'
3  d  5  4  4  9  b'62'
4  e  5  2  2  2  b'62'
5  f  4  0  0  4  b'62'


# decode
binascii.unhexlify(load.loc[1]['F']).decode('utf-8') -->> 'a'

示例

print(binascii.hexlify('HelloWorld'.encode())) --> b'48656c6c6f576f726c64'

print(binascii.unhexlify('48656c6c6f576f726c64'.encode())) --> b'HelloWorld'