我的熊猫有一个数据框
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]
这样做的最佳方法是什么?
答案 0 :(得分:4)
使用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
答案 1 :(得分:0)
尝试此方法。
from pandas import Series,DataFrame
import pandas as pd
import random
df = DataFrame({
'val1': ['a','d','g','j','c','m','g','v'],
'val2':['b','e','h','k','a','e','n','k'],
'val3':['c','f','i','l','q','t','y','l'],
'time':[0,5,7,4,9,2,1,0]})
id = df['time'].idxmax()
max = df['time'][id]
df['timesteps'] = df.apply(lambda x: random.randint(0,max-1),axis = 1)