在 Python 中使用 np.vectorize 映射布尔值

Mapping boolean with np.vectorize in Python

我在 pandas df:

中有这两列
     seconds       min
0              52  0.866667
1               5  0.083333
2              10  0.166667
3               5  0.083333
4              16  0.266667
197           144  2.400000

只有当['seconds']大于60时,我想在['min']列中将秒转换为分钟,否则它应该是空的。

我尝试将 ['min'] 列转换为布尔值:

    x = df['min'] = np.array(df['seconds'].apply(lambda x: True if x >60 else False))

我得到了布尔值,然后我尝试映射它:

    mapping = [df['seconds']/60]
    result = np.vectorize(lambda i:mapping[i], x)

但我得到了 return:

TypeError: 列表索引必须是整数或切片,而不是 str

这是正确的方法还是有其他更简单的解决方案?

输出:

    seconds       min
0              52  
1               5  
2              10  
3               5  
4              16  
197           144  2.400000

您的 'True' 和 'False' 是字符串而不是布尔值。布尔值没有引号

我不确定如何使用您的矢量化方法,但以下方法可行

df.loc[df['seconds']<60,'min'] = 0
df.loc[df['seconds']>60,'min'] = df['seconds'][df['seconds']>60] /60