我有一个 Python 数据框,需要创建一个新列,该列返回当前行下方和不同列中的行的值。以下是数据框:
event event time
pass 10.10
failed block 10.20
failed check 10.21
reception 10.25
pass 17.60
reception 17.65
我需要创建一个名为接收时间的列。 Reception time 将在每次传递后查找第一个接收实例并返回接收的事件时间,如下所示:
event event time reception time
pass 10.10 10.25
failed block 10.20 NA
reception 10.25 NA
pass 17.60 17.65
reception 17.65 NA
通行证和接待处之间可能有 50 条或更多线路。
我是 Python 新手,无法找到解决方案。
最佳答案
df['reception time'] = (df['event time']
.where(df['event'].eq('reception'))
.bfill()
.where(df['event'].eq('pass'))
)
输出:
event event time reception time
0 pass 10.10 10.25
1 failed block 10.20 NaN
2 failed check 10.21 NaN
3 reception 10.25 NaN
4 pass 17.60 17.65
5 reception 17.65 NaN
关于python - 新的数据框列在一行中查找值并从另一列返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69831418/