python - 为什么 Panda 的 df.apply 在按原样返回行时比预期的多调用一次?
问题描述
我观察到 Panda 的数据框的 apply 函数有一些奇怪的行为。
test_df = pd.DataFrame(data={
'A': [1,2,3],
'B': [4,5,6],
'C': [7,8,9]
})
def test(row):
print('Called Test1')
return row # returning row here
def test2(row):
print('Called Test2')
return True # return anything except row
# sample_df = test_df.sample(3, random_state=2)
test_df.apply(test, axis=1)
print('=====================')
test_df.apply(test2, axis=1)
print('Why Test 1 is called 1 time more than test 2.')
输出结果
Called Test1
Called Test1
Called Test1
Called Test1
=====================
Called Test2
Called Test2
Called Test2
问题
- 为什么 apply 函数调用
test()
次数多于 1 次test2()
?这是预期的行为吗?
谷歌 Colab 链接:这里
解决方案
推荐阅读
- nestjs - Nestjs - 如何从 2 个不同的警卫那里获取 req.user?
- android - 在 Flutter firebase_messaging 6.0.13 中未调用 onResume 和 onLaunch
- c# - 如果倒数计时器归零,标签就会消失
- reactjs - 如何使用 Styled-Components 设置链接的样式?
- java - 后端服务器中的 Firebase revokeRefreshTokens 然后 firebase.auth().signOut() 然后尝试登录(谷歌提供商)
- vim - 通过调用make打开quickfix窗口后如何调用函数
- javascript - 我无法在反应钩子中将对象置于 setstate 中
- loops - 使用单元格作为八度中while循环的输出表
- javascript - 删除模态jQuery的父div
- sql-server - 从数据库中获取项目仅返回 en 版本