How to form tuple column from two columns in Pandas

后端 未结 4 1767
天命终不由人
天命终不由人 2020-11-28 02:43

I\'ve got a Pandas DataFrame and I want to combine the \'lat\' and \'long\' columns to form a tuple.


Int64Index         


        
4条回答
  •  無奈伤痛
    2020-11-28 03:00

    I'd like to add df.values.tolist(). (as long as you don't mind to get a column of lists rather than tuples)

    import pandas as pd
    import numpy as np
    
    size = int(1e+07)
    df = pd.DataFrame({'a': np.random.rand(size), 'b': np.random.rand(size)}) 
    
    %timeit df.values.tolist()
    1.47 s ± 38.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
    %timeit list(zip(df.a,df.b))
    1.92 s ± 131 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    

提交回复
热议问题