使用哈希值转换数据框列
Transform dataframe column with a hash value
这是我的数据框。
Student Studendid Student Studendid Student Studendid
0 Stud1 1 0 Stud1 ah274as 0 Stud1 1
1 Stud2 2 1 Stud2 ah474as 1 Stud2 2
2 Stud3 3 2 Stud3 ah454as 2 Stud3 3
3 Stud4 4 hash 3 Stud4 48sdfds hash 3 Stud4 4
4 Stud5 5 -> 4 Stud5 dash241 -> 4 Stud5 5
5 Stud6 6 5 Stud6 asda212 5 Stud6 6
6 Stud7 7 6 Stud7 askdkj2 6 Stud7 7
7 Stud8 8 7 Sud8 kadhh23 7 Stud8 8
8 Stud9 9 8 Stud9 asdhb27 8 Stud9 9
基于学生,我想散列学生证。
我已经尝试过 hash()
函数。不幸的是,我还没有找到任何如何将其散列回去的方法。
我想散列然后再次散列。
有什么方法可以对 Studend 进行散列和散列?
df[Studendid] = df["Student"].hash()
赞@Ch3steR 评论:
This correct assuming every value has a unique "hash value" but there doesn't exist such hash function as of now. Every hash function is collision prone.
# Example for collision
hash(0.1) == hash(230584300921369408)
True
注意: 来自Python 3.3 字符串和字节对象的值在散列过程之前用随机值加盐。这意味着字符串的值被修改为一个随机值,每次您的解释器启动时该值都会改变。 这样做是为了避免 dictionary hash attack
# Example taken martijn's answer:
>>> hash("235")
-310569535015251310
现在,打开一个新会话。
>>> hash("235")
-1900164331622581997
但是如果只有几行数据你可以使用:
使用 helper 字典进行哈希,然后将交换 key:values
映射回 d1
字典并传递给 Series.map
:
d2 = {hash(x):x for x in df['Student']}
d1 = {v:k for k, v in d2.items()}
df['Studendid']= df['Student'].map(d1)
df['orig']= df['Studendid'].map(d2)
print (df)
Student Studendid orig
0 Stud1 6001180169368329239 Stud1
1 Stud2 -1507322317280771023 Stud2
2 Stud3 -2262724814055039076 Stud3
3 Stud4 364063172999472918 Stud4
4 Stud5 8548751638627509914 Stud5
5 Stud6 5647607776109616031 Stud6
6 Stud7 729989721669472240 Stud7
7 Stud8 4828368150311261883 Stud8
8 Stud9 8466663427818502594 Stud9
这是我的数据框。
Student Studendid Student Studendid Student Studendid
0 Stud1 1 0 Stud1 ah274as 0 Stud1 1
1 Stud2 2 1 Stud2 ah474as 1 Stud2 2
2 Stud3 3 2 Stud3 ah454as 2 Stud3 3
3 Stud4 4 hash 3 Stud4 48sdfds hash 3 Stud4 4
4 Stud5 5 -> 4 Stud5 dash241 -> 4 Stud5 5
5 Stud6 6 5 Stud6 asda212 5 Stud6 6
6 Stud7 7 6 Stud7 askdkj2 6 Stud7 7
7 Stud8 8 7 Sud8 kadhh23 7 Stud8 8
8 Stud9 9 8 Stud9 asdhb27 8 Stud9 9
基于学生,我想散列学生证。
我已经尝试过 hash()
函数。不幸的是,我还没有找到任何如何将其散列回去的方法。
我想散列然后再次散列。
有什么方法可以对 Studend 进行散列和散列?
df[Studendid] = df["Student"].hash()
赞@Ch3steR 评论:
This correct assuming every value has a unique "hash value" but there doesn't exist such hash function as of now. Every hash function is collision prone.
# Example for collision
hash(0.1) == hash(230584300921369408)
True
注意: 来自Python 3.3 字符串和字节对象的值在散列过程之前用随机值加盐。这意味着字符串的值被修改为一个随机值,每次您的解释器启动时该值都会改变。 这样做是为了避免 dictionary hash attack
# Example taken martijn's answer:
>>> hash("235")
-310569535015251310
现在,打开一个新会话。
>>> hash("235")
-1900164331622581997
但是如果只有几行数据你可以使用:
使用 helper 字典进行哈希,然后将交换 key:values
映射回 d1
字典并传递给 Series.map
:
d2 = {hash(x):x for x in df['Student']}
d1 = {v:k for k, v in d2.items()}
df['Studendid']= df['Student'].map(d1)
df['orig']= df['Studendid'].map(d2)
print (df)
Student Studendid orig
0 Stud1 6001180169368329239 Stud1
1 Stud2 -1507322317280771023 Stud2
2 Stud3 -2262724814055039076 Stud3
3 Stud4 364063172999472918 Stud4
4 Stud5 8548751638627509914 Stud5
5 Stud6 5647607776109616031 Stud6
6 Stud7 729989721669472240 Stud7
7 Stud8 4828368150311261883 Stud8
8 Stud9 8466663427818502594 Stud9