pandas 散列熊猫数据框中的每个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30143911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hash each value in a pandas data frame
提问by user3664020
In python, I am trying to find the quickest to hash each value in a pandas data frame.
在 python 中,我试图找到对 Pandas 数据框中每个值进行散列的最快方法。
I know any string can be hashed using:
我知道任何字符串都可以使用以下方法进行散列:
hash('a string')
But how do I apply this function on each element of a pandas data frame?
但是如何在 Pandas 数据框的每个元素上应用这个函数呢?
This may be a very simple thing to do, but I have just started using python.
这可能是一件非常简单的事情,但我刚刚开始使用 python。
回答by EdChum
Pass the hashfunction to applyon the strcolumn:
通过该hash功能,apply在str列:
In [37]:
df = pd.DataFrame({'a':['asds','asdds','asdsadsdas']})
df
Out[37]:
a
0 asds
1 asdds
2 asdsadsdas
In [39]:
df['hash'] = df['a'].apply(hash)
df
Out[39]:
a hash
0 asds 4065519673257264805
1 asdds -2144933431774646974
2 asdsadsdas -3091042543719078458
If you want to do this to every element then call applymap:
如果要对每个元素执行此操作,请调用applymap:
In [42]:
df = pd.DataFrame({'a':['asds','asdds','asdsadsdas'],'b':['asewer','werwer','tyutyuty']})
df
Out[42]:
a b
0 asds asewer
1 asdds werwer
2 asdsadsdas tyutyuty
In [43]:
df.applymap(hash)
?
Out[43]:
a b
0 4065519673257264805 7631381377676870653
1 -2144933431774646974 -6124472830212927118
2 -3091042543719078458 -1784823178011532358

