0

I'm trying to write a program to calculate a bunch of statistics based on a data set. I have the names of the statistic, function to calculate the statistic, and place for results in a dataframe. All that is left is how to evaluate the function and put the results in the result column.

I've seen function pointers in python used like

def func():
    #do a thing
    return f
func()

and I've seen applying things across a dataframe like

df['col1'] = 5 + df['col2']

but combining the concepts like below doesn't seem to work saying a series object is not callable. I'm hoping there is a way to do this cleanly without iterating over the rows.

def a():
    #do a thing...
    return 'a'

def b():
    #do a thing
    return 'b'

def c():
    #do a thing
    return 'c'

df = pd.DataFrame([[a,''],[b,''],[c,'']], columns=['func','result'])

                                 func result
0  <function a at 0x000000001775D3C8>
1  <function b at 0x000000001775D438>
2  <function c at 0x000000001775D4A8>


df['result'] = df['func']()

                                 func result
0  <function a at 0x000000001775D3C8>   'a'
1  <function b at 0x000000001775D438>   'b'
2  <function c at 0x000000001775D4A8>   'c'

1 Answers1

0

or you start looking for alternatives and the answer finds you... see this other SO question

df['result'] = df.apply(lambda row: row['func'](), axis=1)