Reputation: 1227
I'm trying to write a lambda-function into a def:-function in order to better understand what is going on in a Python example-script I got. In some function in that script a lambda-function is integrated and I'm wondering what it would look like as def:-statement. (I'm new to using lamda functions) So here is the code:
import numpy as np
import pandas as pd
df = pd.DataFrame({"A": [18,28,29,32,35,36,37,37,39,40,42,42,46,48,54,56,57,61,61,62,63,65], "B": [9,13,17,15,23,21,24,28,26,30,29,36,38,42,40,48,40,51,54,50,51,57]})
a = lambda df: np.corrcoef(df[:,0], df[:,1])[0,1]
print(a)
>>>function <lambda> at 0x0000016896784950> #result of print-statement
def lamb(df):
g = np.corrcoef(df.iloc[:,0],df.iloc[:,1])[0,1]
return g
b = lamb(df)
print(b)
>>>0.974499725153725 #result of print-statement
How do I alter the def lamb(df):
-code so that its print-statement has the same output as the print-statement of the lambda-function?
Upvotes: 0
Views: 103
Reputation: 461
just print the function lamb. this will print the function at location at 0x......
print(lamb)
Upvotes: 0
Reputation: 3521
With
print(a)
You're trying to print a function, to have the same usable output:
print(a(df))
Is what you're looking for
Upvotes: 1
Reputation: 9345
print(a)
prints a function, but
b = lamb(df)
print(b)
print the output of the function
Try print(lamb)
to get the similar result
Upvotes: 2