首页 > 解决方案 > 使用同一 Dataframe 中另一列的 int 作为索引获取列中的列表值

问题描述

我有一个包含两列的熊猫数据框:

df.selection
(...)
1454    5
1458    6
1473    4
1474    4
1487    4
1491    3
1500    6
Name: selection, Length: 117, dtype: int64

df.value_lsts
(...)
1454         [8.4, 16.0, 7.4, 3.96, 17.5, 2.6]
1458       [8.85, 3.25, 5.3, 4.95, 8.14, 11.0]
1473     [9.8, 5.28, 11.67, 15.15, 4.47, 3.06]
1474       [5.5, 2.19, 7.7, 11.98, 28.0, 8.54]
1487      [26.6, 9.74, 7.71, 6.46, 2.28, 7.58]
1491       [6.4, 3.1, 19.92, 4.2, 6.37, 11.05]
1500     [3.0, 22.91, 8.61, 13.58, 6.37, 3.69]
Name: value_lsts, Length: 117, dtype: object

那是一列列表。

我需要的是创建另一列,其值将由以下给出:

value_lsts[df.selection - 1]

例如对于第 1500 行,我们有

df.value_lsts
1500     [3.0, 22.91, 8.61, 13.58, 6.37, 3.69]

df.selection
1500    6

所以返回值为3.69

我已经尝试了一切,但无法提出解决方案。通过 df.selection 列访问正确索引的 pythonic 方式是什么?

非常感谢。皮耶罗

标签: pythonpandaslistdataframeseries

解决方案


请注意,将可变对象放入 DataFrame 可能是一种反模式


如果您确定要实现的目标并确信您需要一列列表 - 您可以像这样解决您的问题:

  1. 使用apply方法:

    df["new_column"] = df.apply(lambda raw: raw.value_lsts[raw.selection -1], axis = 1)
    
  2. 使用列表理解:

    df["new_column"]  = [x[y-1] for x, y in zip(df['value_lsts'], df['selection'])]
    
  3. 使用矢量化函数:

    def get_by_index(value_lsts,selection): # you may use lambda here as well
        return value_lsts[selection-1]
    
    df["new_column"] = np.vectorize(get_by_index) (df['value_lsts'], df['selection'])
    

在我看来,选择哪个选项是可读性和性能之间的权衡。


让我们比较算法性能

创建更大的数据框

df_1 = df.sample(100000, replace=True).reset_index(drop=True)

计时

# 1. apply 
%timeit df_1["new_column"] = df_1.apply(lambda raw: raw.value_lsts[raw.selection-1], axis = 1)
2.77 s ± 94.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# 2. list comprehension:
%timeit df_1["new_column"]  = [x[y-1] for x, y in zip(df_1['value_lsts'], df_1['selection'])] 
33.9 ms ± 1.29 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# 3. vectorized function:
%timeit df_1["new_column"] = np.vectorize(get_by_index) (df_1['value_lsts'], df_1['selection'])
12 ms ± 302 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# 4. solution proposed by @anky using lookup 
%%timeit 
u = pd.DataFrame(df_1['value_lsts'].tolist(),index=df_1.index) #helper dataframe
df_1['selected_value'] = u.lookup(u.index,df_1['selection']-1)
51.9 ms ± 865 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

如果您不确定是否真的需要一列列表 - 您可以阅读有关将列表列拆分为多个列的正确方法


推荐阅读