0

Say I have a DF defined as variable DF1:

 Words Score The man 10 A Plan 20 Panama 30 

And say I have a function:

def func(w, df): pattern = re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE) if pattern.search(df): return True else: return False 

How do I pass each row of DF1, specifically the columns 'Words', to the argument within the function?

EDIT: Ubuntu's answer is what I would normally use but I need to self reference the DF in my function

1
  • Ok, edited -- but how can I pass my DF to the function! Commented Oct 2, 2014 at 21:42

1 Answer 1

1

You could use the Series.apply method:

df1['Words'].apply(func) 

If you wish to pass more positional arguments to func, use the args keyword parameter:

df1['Words'].apply(func, args=(df,)) 

or, to pass df as a keyword argument:

df1['Words'].apply(func, df=df) 

since, per the docs, "[a]dditional keyword arguments [to apply] will be passed as keywords to the function".

Sign up to request clarification or add additional context in comments.

5 Comments

@DSM: Your answer is more complete than mine; shall we swap delete states?
hmmm this gets me sort of the way here but not quite -- how can I apply that if I need to actually CALL the dataframe in the arguement? Edited OP above
so if the function was to see whether a word (i.e. the w argument in the function) was found within df1['Words'] -- is there a way to do this?
Hi Unutbu -- would you mind explaining a little bit more about this? Unfamiliar with how this works. What exactly is df=df representing? Say I wanted to use this method is it called like this: df1['Words'].apply(func('panama'), df1=df1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.