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?
print(a(df))in the same way you hadprint(lamb(df)). Both a and lamb are functions.print(lamb)?