0

how to use variable name inside pandas apply function . Need to pass function name dynamically to apply function.

func_name='int'
df['column3'] = df['column3'].apply(func_name)
  • Does this answer your question? [Calling a function of a module by using its name (a string)](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) – Ch3steR Sep 10 '20 at 04:23
  • What is the purpose of doing that? Let us know your end goal, there might be better alternatives. You can access the variable name within a *lambda* function. If you want to pass arguments use *args* parameter. – Bharath M Shetty Sep 10 '20 at 04:37

1 Answers1

0

I believe what you're looking for is globals() :

x = 3
def func(x):
    return x**2
var = 'func'
print(globals()[var](x))

This code will print 9.