gpt4 book ai didi

python - 根据另一个排序列表中的值索引创建新列

转载 作者:行者123 更新时间:2023-12-03 19:13:37 25 4
gpt4 key购买 nike

我在 Pandas 中有一个数据框

val1 val2 val3 time 
a b c 0
d e f 5
g h i 7
j k l 4
c a q 9
m e t 2
g n y 1
v k l 0

timesteps = [0, 3, 8 ]

我想创建一个新列,它是来自 timesteps 的元素的最大值。低于 row["time"]例如,这里的新列将是 [0,3,3,3,8,0,0,0]
这样做的最佳方法是什么?

最佳答案

使用 pd.cut() :

timesteps = [0, 3, 8]
bins=timesteps+[df.time.max()]
#[0, 3, 8, 9]
pd.cut(df.time,bins=bins,labels=timesteps,include_lowest=True)
0    0
1 3
2 3
3 3
4 8
5 0
6 0
7 0

关于python - 根据另一个排序列表中的值索引创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56454959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com