假设我有一个 Pandas 数据框 df :

start_time   Event
0            0
1            0
2            0
3            0
4            0
5            0
6            0
7            0
8            0
9            0

当相应的 Event 位于两个值之间时,我想将 start_time 列的值设置为 -1,所以我定义了这个函数:

def test(time):
    if (time['start_time'] >= 5) and (time['start_time'] <= 8):
        return -1
    else:
        return time

要将其应用于事件列,我执行以下操作:

df[['Event']] = df[['Event']].apply(test,axis=1)

产生此错误:KeyError: ('start_time', 'occurred at index 0')
为什么会这样?应该是一个简单的修复。

最佳答案

简单地做:

df['Event'] = df.apply(test, axis=1)['Event']

关于python - Pandas KeyError : 'occurred at index 0' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59848681/

10-12 17:00
查看更多