Python Forum
Issue in .apply function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Issue in .apply function (/thread-23226.html)



Issue in .apply function - fullstop - Dec-17-2019

I have a simple code as below
import pandas as pd
matrix = [(222, 34, 23),
          (333, 31, 11),
          (444, 16, 21),
          (555, 32, 22),
          (666, 33, 27),
          (777, 35, 11)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, columns=list('abc'))

def doubleData(x):
   print(x)
   return x * 2

modDfObj=dfObj.apply(doubleData)
I this code I am using .apply function to dfObj. Since by default axis = 0, I am transferring my 3 columns one by one as series to function doubleData. But when I print the columns I am sending to function as a series, print(x) in doubleData gets executed 4 times. I check the output and found that first column is printing 2 times. This is strange to me since each column should be send to function only once.