我正在观察熊猫数据框的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
问题
test()
多调用test2()
1次?这是预期的行为吗?Google Colab链接:here