当返回原来的行时,为什么熊猫的df.apply被调用的时间比预期的多一倍?

时间:2019-06-07 19:31:19

标签: python pandas dataframe

我正在观察熊猫数据框的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

问题

  1. 为什么apply函数比test()多调用test2() 1次?这是预期的行为吗?

Google Colab链接:here

0 个答案:

没有答案