Create hash value for each row of data with selected columns in dataframe in python pandas

前端 未结 6 848
北恋
北恋 2020-12-06 10:18

I have asked similar question in R about creating hash value for each row of data. I know that I can use something like hashlib.md5(b\'Hello World\').hexdigest()

6条回答
  •  广开言路
    2020-12-06 11:13

    Or simply:

    df.apply(lambda x: hash(tuple(x)), axis = 1)
    

    As an example:

    import pandas as pd
    import numpy as np
    df = pd.DataFrame(np.random.rand(3,5))
    print df
    df.apply(lambda x: hash(tuple(x)), axis = 1)
    
         0         1         2         3         4
    0  0.728046  0.542013  0.672425  0.374253  0.718211
    1  0.875581  0.512513  0.826147  0.748880  0.835621
    2  0.451142  0.178005  0.002384  0.060760  0.098650
    
    0    5024405147753823273
    1    -798936807792898628
    2   -8745618293760919309
    

提交回复
热议问题