![](/img/trans.png)
[英]Calculate value using previously-calculated value (from the same column) and value from another column in a Pandas Dataframe
[英]Get a list value in a column using as index an int from another column in the same Dataframe
我有一个 pandas dataframe 有两列:
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 方式是什么?
非常感谢你。 皮耶罗
请注意, 将可变对象放入 DataFrame 可能是一种反模式
如果您确定要实现的目标并确信您需要一列列表 - 您可以这样解决您的问题:
使用apply
方法:
df["new_column"] = df.apply(lambda raw: raw.value_lsts[raw.selection -1], axis = 1)
使用列表理解:
df["new_column"] = [x[y-1] for x, y in zip(df['value_lsts'], df['selection'])]
使用矢量化 function:
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)
如果您不确定您是否真的需要一列列表 - 您可以阅读有关将列表列拆分为多列的正确方法。
在将一系列列表转换为 dataframe 之后,您也可以在此处使用df.lookup
(请注意,python 索引从 0 开始,因此应根据您的逻辑使用selection-1
)
u = pd.DataFrame(df['value_list'].tolist(),index=df.index) #helper dataframe
df['selected_value'] = u.lookup(u.index,df['selection']-1)
print(df)
selection value_list selected_value
1454 5 [8.4, 16.0, 7.4, 3.96, 17.5, 2.6] 17.50
1458 6 [8.85, 3.25, 5.3, 4.95, 8.14, 11.0] 11.00
1473 4 [9.8, 5.28, 11.67, 15.15, 4.47, 3.06] 15.15
1474 4 [5.5, 2.19, 7.7, 11.98, 28.0, 8.54] 11.98
1487 4 [26.6, 9.74, 7.71, 6.46, 2.28, 7.58] 6.46
1491 3 [6.4, 3.1, 19.92, 4.2, 6.37, 11.05] 19.92
1500 6 [3.0, 22.91, 8.61, 13.58, 6.37, 3.69] 3.69
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.