简体   繁体   English

给定 pandas DataFrame 中一列中的值列表,如何从同一行中的另一列中获取 output 中的值?

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

The problem is simple, the input is a list of non-container objects ( int , str etc.), all elements inside the list are contained inside a column in a DataFrame , the task is, for each element inside the list, find the object (only its value, not the array) in another column in the same row.问题很简单,输入是一个非容器对象列表( intstr等),列表中的所有元素都包含在DataFrame的列中,任务是,对于列表中的每个元素,找到object(只是它的值,不是数组)在同一行的另一列中。

The problem will be better demonstrated in code:该问题将在代码中得到更好的证明:

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))

As you can see I can already do this, but I am using a for loop, and the performance isn't very impressive, I know pandas and numpy are all about vectorization, I wonder is there a built-in way to do this...如您所见,我已经可以做到这一点,但我正在使用 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)

So what is a vectorized way to achieve the same result as the method demonstrated in the code?那么有什么向量化的方式可以实现和代码中演示的方法一样的结果呢?

A vectorized way would be to construct a Series:矢量化的方法是构造一个系列:

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

Output: '9716bcea' Output: '9716bcea'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在给定条件的情况下用 pandas dataframe 中的另一列替换一列的值 - How to replace the values of a column with another column in pandas dataframe given a condition 熊猫:从另一个数据框中的列值插入数据框中的行 - pandas: insert rows in a dataframe from column values in another dataframe 根据给定列 pandas 中的缺失值,将行从一个 dataframe 添加到另一个 - Add rows from one dataframe to another based on missing values in a given column pandas 来自 Pandas DataFrame 的 Select 行与另一个 DataFrame 中的列值完全相同 - Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 如何根据 pandas dataframe 中另一列的多个值在一列中创建值列表? - How do I create a list of values in a column from several values from another column in a pandas dataframe? Gapfill pandas dataframe 在另一个 dataframe 中使用同一列中的值 - Gapfill pandas dataframe using values from same column in another dataframe 在Pandas数据框中查找具有相同列值的行 - Finding rows with same column values in pandas dataframe pandas dataframe 按作为列表的列的值过滤行 - pandas dataframe filter rows by values of column that is a list 从 Pandas Dataframe 中选择一列中具有相同值而另一列仅缺失的行 - Selecting rows from Pandas Dataframe with same values in one column that have only missing in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM