给定 pandas DataFrame 中一列中的值列表,如何从同一行中的另一列输出值?

Given a list of values in a column in pandas DataFrame, how to output values from another column in the same rows?

问题很简单,输入的是一个非容器对象的列表(intstr等),列表里面的所有元素都包含在一个DataFrame,任务是,对于列表中的每个元素,在同一行的另一列中找到对象(只是它的值,而不是数组)。

问题会在代码中得到更好的体现:

from pandas import DataFrame
digits = '0123456789abcdef'
df = DataFrame([(a,b) for a, b in zip(digits, range(16))], columns=['hex', 'dec'])
df
df.loc[df.dec == 12, 'hex']
df.loc[df.dec == 12, 'hex'].values[0]
import random
eight = random.sample(range(16), 8)
eight
fun = lambda x: df.loc[df.dec == x, 'hex'].values[0]
''.join(fun(i) for i in eight)
''.join(map(fun, eight))

如您所见,我已经可以做到这一点,但我使用的是 for 循环,性能不是很出色,我知道 pandasnumpy 都是关于矢量化的,我想知道是否有内置的方法可以做到这一点...

In [1]: from pandas import DataFrame

In [2]: digits = '0123456789abcdef'

In [3]: df = DataFrame([(a,b) for a, b in zip(digits, range(16))], columns=['hex', 'dec'])

In [4]: df
Out[4]:
   hex  dec
0    0    0
1    1    1
2    2    2
3    3    3
4    4    4
5    5    5
6    6    6
7    7    7
8    8    8
9    9    9
10   a   10
11   b   11
12   c   12
13   d   13
14   e   14
15   f   15

In [5]: df.loc[df.dec == 12, 'hex']
Out[5]:
12    c
Name: hex, dtype: object

In [6]: df.loc[df.dec == 12, 'hex'].values[0]
Out[6]: 'c'

In [7]: import random

In [8]: eight = random.sample(range(16), 8)

In [9]: eight
Out[9]: [9, 7, 1, 6, 11, 12, 14, 10]

In [10]: fun = lambda x: df.loc[df.dec == x, 'hex'].values[0]

In [11]: ''.join(fun(i) for i in eight)
Out[11]: '9716bcea'

In [12]: ''.join(map(fun, eight))
Out[12]: '9716bcea'

In [13]: %timeit ''.join(fun(i) for i in eight)
2.34 ms ± 136 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [14]: %timeit ''.join(map(fun, eight))
2.34 ms ± 134 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

那么什么是矢量化的方式来实现与代码中演示的方法相同的结果?

向量化的方法是构造一个系列:

series = df.set_index('dec')['hex']
''.join(series[eight])

输出:'9716bcea'