创建一个包含两个熊猫数据栏的字典最有效的方法是什么?

时间:2022-06-22 22:24:10

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

最有效的方式是什么?

data =

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']?

如字母表[1:'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e']?

2 个解决方案

#1


62  

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)

速度比较(使用Wouter方法)

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

#2


24  

I found a faster way to solve the problem, at least on realistically large datasets using: df.set_index(KEY).to_dict()[VALUE]

我找到了一种更快的方法来解决这个问题,至少在使用df.set_index(KEY).to_dict()[VALUE]的实际大型数据集上是这样

Proof on 50,000 rows:

证明在50000行:

df = pd.DataFrame(np.random.randint(32, 120, 100000).reshape(50000,2),columns=list('AB'))
df['A'] = df['A'].apply(chr)

%timeit dict(zip(df.A,df.B))
%timeit pd.Series(df.A.values,index=df.B).to_dict()
%timeit df.set_index('A').to_dict()['B']

Output:

输出:

100 loops, best of 3: 7.04 ms per loop  # WouterOvermeire
100 loops, best of 3: 9.83 ms per loop  # Jeff
100 loops, best of 3: 4.28 ms per loop  # Kikohs (me)

#1


62  

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)

速度比较(使用Wouter方法)

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

#2


24  

I found a faster way to solve the problem, at least on realistically large datasets using: df.set_index(KEY).to_dict()[VALUE]

我找到了一种更快的方法来解决这个问题,至少在使用df.set_index(KEY).to_dict()[VALUE]的实际大型数据集上是这样

Proof on 50,000 rows:

证明在50000行:

df = pd.DataFrame(np.random.randint(32, 120, 100000).reshape(50000,2),columns=list('AB'))
df['A'] = df['A'].apply(chr)

%timeit dict(zip(df.A,df.B))
%timeit pd.Series(df.A.values,index=df.B).to_dict()
%timeit df.set_index('A').to_dict()['B']

Output:

输出:

100 loops, best of 3: 7.04 ms per loop  # WouterOvermeire
100 loops, best of 3: 9.83 ms per loop  # Jeff
100 loops, best of 3: 4.28 ms per loop  # Kikohs (me)