So I have a bunch of functions I want to create with different parameters. One of the parameters df
will be provided by the caller of these functions. I thought I had it figured out but when I actually used it every function created had the same parameters, the last combination in the list comprehension sequence. weird.
from itertools import product
feature_functions = {
**{f'{col}{i}': lambda x: createFeature(df=x, i=i, col=col, name=f'{col}{i}')
for col, i in product(['New', 'Lost', 'Change'], list(range(1, 31)))},
like I said, I thought this was pretty slick but when I used it like so:
feature_functions['New1'](df)
I got this result, meaning it was using the 'Change' and 30 for each lambda function:
# feature pd.Series:
0 NaN
...
4593 1.002706
Name: Change30, Length: 4594, dtype: float64
I tried several things, but nothing changed. How am I using this dictionary comprehension wrong?
EDIT: By the way, one thing that I did to verify it was right, was put the lambda x: ...
in quotes, then I could just print it all out and it looked pretty good. so, somehow the lambda is getting in the way? I did try wrapping it in (lambda x: ...)
but that did nothing.
{'New1': "lambda x: createFeature(df=x, i=1, col=New, name='New1')",
'New2': "lambda x: createFeature(df=x, i=2, col=New, name='New2')",
'New3': "lambda x: createFeature(df=x, i=3, col=New, name='New3')",
'New4': "lambda x: createFeature(df=x, i=4, col=New, name='New4')",
...
}