什么是最有效的方式来创建两个大 pandas 数据框列的字典? [英] What is the most efficient way to create a dictionary of two pandas Dataframe columns?

查看:106
本文介绍了什么是最有效的方式来创建两个大 pandas 数据框列的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据框架的最有效的方式是什么?



data =

 职位信
1 a
2 b
3 c
4 d
5 e

成为一个字典,如 alphabet [1:'a',2:'b',3:'c',4: 'd',5:'e']

解决方案

  In [9]:Series(df.Letter.values,index = df.Position).to_dict()
Out [9]:{1:'a',2:'b',3:' c',4:'d',5:'e'}

速度比较(使用Wouter的方法)

 在[6]中:df = DataFrame(randint(0,10,10000).reshape(5000,2) column = list('AB')

在[7]中:%timeit dict(zip(df.A,df.B))
1000循环,最好的3:1.27 ms每循环

在[8]中:%timeit系列(df.A.values,index = df.B).to_dict()
1000循环,最好3:987 us每循环


What is the most efficient way to organise the following pandas Dataframe:

data =

Position    Letter
1           a
2           b
3           c
4           d
5           e

into a dictionary like alphabet[1 : 'a', 2 : 'b', 3 : 'c', 4 : 'd', 5 : 'e']?

解决方案

In [9]: Series(df.Letter.values,index=df.Position).to_dict()
Out[9]: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

Speed comparion (using Wouter's method)

In [6]: df = DataFrame(randint(0,10,10000).reshape(5000,2),columns=list('AB'))

In [7]: %timeit dict(zip(df.A,df.B))
1000 loops, best of 3: 1.27 ms per loop

In [8]: %timeit Series(df.A.values,index=df.B).to_dict()
1000 loops, best of 3: 987 us per loop

这篇关于什么是最有效的方式来创建两个大 pandas 数据框列的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆