Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue in .apply function
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to apply function lower() to the list? Toltimtixma 2 756 Feb-10-2023, 05:15 PM
Last Post: Toltimtixma
  IF statement to apply at each date illmattic 2 2,598 Apr-08-2021, 12:31 PM
Last Post: illmattic
  Function won't apply dynamically in timeseries illmattic 1 1,708 Jan-08-2021, 03:15 PM
Last Post: stullis
  How to apply VLookup formula jonzee 2 3,501 Jan-12-2020, 04:16 PM
Last Post: Clunk_Head
  Converting Smiles to Fingerprint with pandas iterrows/apply-function: Jompie96 0 2,462 Jul-04-2019, 01:50 PM
Last Post: Jompie96
  Apply a function with pandas Jompie96 2 2,181 Jun-13-2019, 07:04 PM
Last Post: Jompie96
  Issue with a function ebolisa 1 1,792 Jan-05-2019, 11:23 AM
Last Post: buran
  return issue in a function PyPhanman 2 3,419 Apr-28-2017, 07:48 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020